C++ parsing problem

P

puzzlecracker

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,
 
E

Eric Pruneau

puzzlecracker said:
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!
 
P

puzzlecracker

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!

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

Eric Pruneau

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?
 
P

puzzlecracker

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.
 

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,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top