473,287 Members | 3,295 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

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

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
4 2738
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


"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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: ip4ram | last post by:
I used to work with C and have a set of libraries which allocate multi-dimensional arrays(2 and 3) with single malloc call. data_type **myarray =...
7
by: simkn | last post by:
Hello, I'm writing a function that updates an array. That is, given an array, change each element. The trick is this: I can't change any elements until I've processed the entire array. For...
18
by: bsder | last post by:
Hi, Can anyone please tell me how to calculate the size of the following 4-dimensional array, and now to use qsort for sorting on this array? double sp = { 4.0, 5.0, 6.0 }; double spa = { {...
4
by: rmorvay | last post by:
I have a requirement to search a multi-dimensional array for an item, then delete the item and "reset" the array so that their are no gaps in the resulting array. I have been trying to figure out...
0
by: fmlamela | last post by:
Hi, I have to create a bi-dimensional matrix, where each row is a int array of a specific size. For adding a new row, I use the ArrayList.Add (int ) command. I would like now to convert this...
17
by: john | last post by:
All: I'm a long-time developer, new to PHP.... Is there an idiom used in PHP to construct SQL statments from $_POST data? I would guess that in many applications, the data read from $_POST...
2
by: barker7 | last post by:
I use a simple double array plus a variable to store the row size to represent two dimensional data. I need to quickly copy this data to a two dimensional array: double. Currently I iterate...
5
by: Mukesh | last post by:
Hi, I am using framework 2.0. I am writing a foreach loop that will extract single dimensional arrays out of double dimensional array. I am trying writing something like this. string ...
152
by: vippstar | last post by:
The subject might be misleading. Regardless, is this code valid: #include <stdio.h> void f(double *p, size_t size) { while(size--) printf("%f\n", *p++); } int main(void) { double array = { {...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.