C++ parsing problem

Discussion in 'C++' started by puzzlecracker, Jun 28, 2008.

  1. I have the following string pattern string Name, followed by n number
    of strings, followed by 2 integers. Such as

    char * needToParse=" Name, str_1, str_2,...,str_n, integer1,
    integer2";

    parse it so that the following 4 variables contain these values:
    char *name=Name;
    char * entry= str1+" "+str2+" "+str_n;

    int i1=interger1;
    int i2=interger2,
    puzzlecracker, Jun 28, 2008
    #1
    1. Advertising

  2. puzzlecracker

    Eric Pruneau Guest

    "puzzlecracker" <> a écrit dans le message de news:
    ...
    > I have the following string pattern string Name, followed by n number
    > of strings, followed by 2 integers. Such as
    >
    > char * needToParse=" Name, str_1, str_2,...,str_n, integer1,
    > integer2";
    >
    > parse it so that the following 4 variables contain these values:
    > char *name=Name;
    > char * entry= str1+" "+str2+" "+str_n;
    >
    > int i1=interger1;
    > int i2=interger2,


    here a simple way to do that

    int main()
    {
    string parse = "Parse me,str1,str2,str3,4,6";

    int beg_pos = 0;
    int end_pos = parse.find(',');
    vector<string> parsed;
    while(end_pos > 0)
    {
    parsed.push_back(parse.substr(beg_pos, end_pos-beg_pos));
    beg_pos = end_pos+1;
    end_pos = parse.find(',', beg_pos);
    }
    return 0;
    }

    the string is parsed and results are stored in a vector. Ok I didn't convert
    the integers but I'm sure you can find a way to do this!

    -----------------------

    Eric Pruneau
    Eric Pruneau, Jun 28, 2008
    #2
    1. Advertising

  3. On Jun 28, 1:51 pm, "Eric Pruneau" <> wrote:
    > "puzzlecracker" <> a écrit dans le message de news:
    > ...
    >
    > > I have the following string pattern string Name, followed by n number
    > > of strings, followed by 2 integers. Such as

    >
    > > char * needToParse=" Name, str_1, str_2,...,str_n, integer1,
    > > integer2";

    >
    > > parse it so that the following 4 variables contain these values:
    > > char *name=Name;
    > > char * entry= str1+" "+str2+" "+str_n;

    >
    > > int i1=interger1;
    > > int i2=interger2,

    >
    > here a simple way to do that
    >
    > int main()
    > {
    > string parse = "Parse me,str1,str2,str3,4,6";
    >
    > int beg_pos = 0;
    > int end_pos = parse.find(',');
    > vector<string> parsed;
    > while(end_pos > 0)
    > {
    > parsed.push_back(parse.substr(beg_pos, end_pos-beg_pos));
    > beg_pos = end_pos+1;
    > end_pos = parse.find(',', beg_pos);
    > }
    > return 0;
    >
    > }
    >
    > the string is parsed and results are stored in a vector. Ok I didn't convert
    > the integers but I'm sure you can find a way to do this!
    >
    > -----------------------
    >
    > Eric Pruneau


    I am thinking along the lines of sscanf without skipping spaces?
    puzzlecracker, Jun 29, 2008
    #3
  4. puzzlecracker

    Eric Pruneau Guest


    > I am thinking along the lines of sscanf without skipping spaces?


    And how will you know how many string there is between the name and the
    frist integer?


    ----------------------

    Eric Pruneau
    Eric Pruneau, Jun 30, 2008
    #4

  5. > And how will you know how many string there is between the name and the
    > frist integer?



    You wouldn't know, it's a undetermined number of strings between the
    name and the last two integers.
    puzzlecracker, Jul 20, 2008
    #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. GIMME
    Replies:
    2
    Views:
    873
    GIMME
    Feb 11, 2004
  2. Naren
    Replies:
    0
    Views:
    578
    Naren
    May 11, 2004
  3. Christopher Diggins
    Replies:
    0
    Views:
    608
    Christopher Diggins
    Jul 9, 2007
  4. Christopher Diggins
    Replies:
    0
    Views:
    430
    Christopher Diggins
    Jul 9, 2007
  5. John Levine
    Replies:
    0
    Views:
    727
    John Levine
    Feb 2, 2012
Loading...

Share This Page