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

V

Venkat

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
 
J

Jon Bell

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 said:

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

string Single;
vector said:
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;
 
V

Venkat

Jon Bell said:
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 said:

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

string Single;
vector said:
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;

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
 
J

Jon Bell

Venkat said:
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;
}
 
V

Venkat

Jon Bell said:
#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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top