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

Discussion in 'C++' started by Venkat, Dec 5, 2003.

  1. Venkat

    Venkat Guest

    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
    Venkat, Dec 5, 2003
    #1
    1. Advertising

  2. Venkat

    Jon Bell Guest

    In article <1070598167.460405@sj-nntpcache-5>,
    Venkat <> 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 <> Presbyterian College
    Dept. of Physics and Computer Science Clinton, South Carolina USA
    Jon Bell, Dec 5, 2003
    #2
    1. Advertising

  3. Venkat

    Venkat Guest

    "Jon Bell" <> wrote in message
    news:bqp3h4$4pr$...
    > In article <1070598167.460405@sj-nntpcache-5>,
    > Venkat <> 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 <> 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
    Venkat, Dec 5, 2003
    #3
  4. Venkat

    Jon Bell Guest

    In article <1070603653.510583@sj-nntpcache-3>,
    Venkat <> 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 <> Presbyterian College
    Dept. of Physics and Computer Science Clinton, South Carolina USA
    Jon Bell, Dec 5, 2003
    #4
  5. Venkat

    Venkat Guest

    "Jon Bell" <> wrote in message
    news:bqpcko$6un$...
    > 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
    Venkat, Dec 5, 2003
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sydex
    Replies:
    12
    Views:
    6,430
    Victor Bazarov
    Feb 17, 2005
  2. Alex
    Replies:
    2
    Views:
    1,195
  3. Eric Layman
    Replies:
    3
    Views:
    607
    Rad [Visual C# MVP]
    Apr 14, 2007
  4. Replies:
    5
    Views:
    414
    James Kanze
    Jun 27, 2008
  5. Replies:
    7
    Views:
    453
Loading...

Share This Page