By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,117 Members | 2,178 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,117 IT Pros & Developers. It's quick & easy.

How do copy Strings from a single dimensional array to double dimensional array

P: n/a
Hi All,

I need to copy strings from a single dimensional array to a double
dimensional array.

Here is my program.

#include <stdio.h>
#include <stdlib.h>

main()
{
char Single[255];
int i=10;
char Double[255][255];

for(int j=0; j<10; j++)
{
printf("Enter a string\n");
scanf("%s",Single);

//Here i need to copy the input String into Double........
}

//Here i need to display the 10 string thus inputted by the user in the
above loop using the Double array.
}
Could some one please let me know how can i go about it.

Thanks In Advance,
Venkat


Jul 22 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
In article <1070598167.460405@sj-nntpcache-5>,
Venkat <ve*******@yahoo.com> wrote:

I need to copy strings from a single dimensional array to a double
dimensional array.
My advice is to use a vector of strings instead of a two-dimensional array
of chars, and C++ style I/O.
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <vector>
#include <string>
main()
int main()
{
char Single[255];
int i=10;
char Double[255][255];
string Single;
vector<string> Double(10); // you need only ten strings, right?
for(int j=0; j<10; j++)
{
printf("Enter a string\n");
scanf("%s",Single);
cout << "Enter a string\n";
getline (cin, Single); // assuming you want an entire line
// including blank spaces //Here i need to copy the input String into Double........
Double[j] = Single;
}

//Here i need to display the 10 string thus inputted by the user in the
above loop using the Double array.
for (int j = 0; j < 10; j++)
{
cout << Double[j] << '\n';
}

return 0;
}


--
Jon Bell <jt*******@presby.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
Jul 22 '05 #2

P: n/a


"Jon Bell" <jt*******@presby.edu> wrote in message
news:bq**********@jtbell.presby.edu...
In article <1070598167.460405@sj-nntpcache-5>,
Venkat <ve*******@yahoo.com> wrote:

I need to copy strings from a single dimensional array to a double
dimensional array.


My advice is to use a vector of strings instead of a two-dimensional array
of chars, and C++ style I/O.
#include <stdio.h>
#include <stdlib.h>


#include <iostream>
#include <vector>
#include <string>
main()


int main()
{
char Single[255];
int i=10;
char Double[255][255];


string Single;
vector<string> Double(10); // you need only ten strings, right?
for(int j=0; j<10; j++)
{
printf("Enter a string\n");
scanf("%s",Single);


cout << "Enter a string\n";
getline (cin, Single); // assuming you want an entire line
// including blank spaces
//Here i need to copy the input String into Double........


Double[j] = Single;
}

//Here i need to display the 10 string thus inputted by the user in the
above loop using the Double array.


for (int j = 0; j < 10; j++)
{
cout << Double[j] << '\n';
}

return 0;
}


--
Jon Bell <jt*******@presby.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA


Hi Jon,

Thanks for the timely response. But let me tell you my requirement.
I am reading line by line from a file and than parsing the each read line to
get a particular string and than need to store them.

Here is the text file

File1.txt

The version number is 3.3
Table Countries
{4534-6576-4234-3421ACD},FRANCE,True,321
{4353-7564-4353-5345AFB},UK,False,546
{4234-7678-4534-5645CBD},ITALY,True,675

Table Capitals
{1234-5678-3930-4567-392ACD}, PARIS,True,456
{4567-3432-7899-3930-4567EAC},LONDON,False,560
{7384-6970-3094-2034-3423CAF},ROME,True,394

Table Names
{2435-5435-7676-4546-123434}, John,True,566
{4543-2345-2345-4565-456565},Julie,True,565
{3454-4566-4565-1234-454545},Lucie,False,766
Now from the able file i need to extract the capitals i.e. PARIS, LONDON and
ROME and place them in a vector as you said and display them.

I used file operations to extract them and store them in a character array
using pointers.

Here is my app

// CSVRead.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <vector>
#include <string>
#include <Winsock2.h>
using namespace std;
int main(int argc, char* argv[])
{
char File1[255];
char line[255];
char *subline;
char *tag;
char *ddi;
char tagarray[255];// for storing each capital and displaying it
char tableCapitals[22] = "Table Capitals";
char tableNames[30] = "Table Names";

strcpy(CSVFile,"C:\\File.txt");
fflush(stdin);
FILE * fp1 = fopen(File1, "r");

// Assuming this Code will take the file pointer to the location where Table
Capitals is located
while(fgets(line,22,fp1) != NULL)
{

if(!stricmp(tableTag,line))
break;
}
//to skip 1 line.
fgets(line,255,fp1);

//File pointer is pointing to the line after Table Capital

while(fgets(line,255,fp1) != NULL)
{

//Break when the string Table Names is in the line read from the file1
if(strstr(line,tableNames))
break;

// This code fetches me the capitals and i can print them using
tagarray.

subline = strchr(line,',');
tag = tagarray;

if(subline == NULL)
break;

//Copying from subline to tag(pointers)
while(*subline != '\0')
{
if(*subline == ',')
subline++;

*tag = *subline;
subline++;
tag++;

if(*subline == ',')
break;
}

*tag = '\0';

printf("The capitalis %s and its length is
%d\n",tagarray,strlen(tagarray));

}
fclose(fp1);

return 0;

}
Is it possible to use string and vector in this case instead of single and
double dimensional array. If possible could please tell me how to go about
it.
Regards,
Venkat



Jul 22 '05 #3

P: n/a
In article <1070603653.510583@sj-nntpcache-3>,
Venkat <ve*******@yahoo.com> wrote:
[snip]
Here is the text file

File1.txt

The version number is 3.3
Table Countries
{4534-6576-4234-3421ACD},FRANCE,True,321
{4353-7564-4353-5345AFB},UK,False,546
{4234-7678-4534-5645CBD},ITALY,True,675

Table Capitals
{1234-5678-3930-4567-392ACD}, PARIS,True,456
{4567-3432-7899-3930-4567EAC},LONDON,False,560
{7384-6970-3094-2034-3423CAF},ROME,True,394

Table Names
{2435-5435-7676-4546-123434}, John,True,566
{4543-2345-2345-4565-456565},Julie,True,565
{3454-4566-4565-1234-454545},Lucie,False,766
Now from the able file i need to extract the capitals i.e. PARIS, LONDON and
ROME and place them in a vector as you said and display them.


#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;

int main()
{
ifstream inFile ("File1.txt");

string line;
while (getline (inFile, line)
&& line.find("Table Capitals") == string::npos)
{
// skip lines until we reach "Table Capitals"
}

vector<string> capitals; // initial size of vector is 0

// now read until we reach a blank line

while (getline (inFile, line) && line != "")
{
int comma1Pos = line.find(',');
int comma2Pos = line.find(',', comma1Pos+1);
int numChars = comma2Pos - comma1Pos - 1;
string capital = line.substr(comma1Pos+1, numChars);
capitals.push_back(capital); // the vector grows as necessary
}

inFile.close();

// now display the capitals that we found

for (int k = 0; k < capitals.size(); ++k)
{
cout << capitals[k] << endl;
}

return 0;
}

--
Jon Bell <jt*******@presby.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
Jul 22 '05 #4

P: n/a
"Jon Bell" <jt*******@presby.edu> wrote in message
news:bq**********@jtbell.presby.edu...
In article <1070603653.510583@sj-nntpcache-3>, #include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;

int main()
{
ifstream inFile ("File1.txt");

string line;
while (getline (inFile, line)
&& line.find("Table Capitals") == string::npos)
{
// skip lines until we reach "Table Capitals"
}

vector<string> capitals; // initial size of vector is 0

// now read until we reach a blank line

while (getline (inFile, line) && line != "")
{
int comma1Pos = line.find(',');
int comma2Pos = line.find(',', comma1Pos+1);
int numChars = comma2Pos - comma1Pos - 1;
string capital = line.substr(comma1Pos+1, numChars);
capitals.push_back(capital); // the vector grows as necessary
}

inFile.close();

// now display the capitals that we found

for (int k = 0; k < capitals.size(); ++k)
{
cout << capitals[k] << endl;
}

return 0;
}


That was great Jon it works perfectly. Thank you once again.
With high regards,
Venkat

Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.