What is the easiest way of parsing a comma separated list, where there can be zero elements between each token. The cstring could look like
1, 3, 4, 5, 6, 7, 8, ....
But could also look like
, , , , , , , , , ...
I've tried something like:
char *original = "1, 3, 4, 5, 6, 7, 8, ...."
char *tok = strtok(original," ,")
while(tok!=NULL){
while(*tok!='\0'){
//dostuff
tok++;
}
tok=strtok(NULL," ,");
}
This apparently only works, if there are elements between the comma's, for instance I've noticed that the first item list will be skipped if there are no elements.
I've tried other solutions like strchr(), but this gets very ugly, and I think there is an easier way.
Thanks
Update:
After some testing I noticed that tokenizing on "," seemed to work, on all cases except if the first item was missing. So I'm pulling that out as a special case.
char *original = "1, 3, 4, 5, 6, 7, 8, ...."
if(*original==',')
//dostuff
char *tok = strtok(original,",")
while(tok!=NULL){
while(*tok!='\0'){
//dostuff
tok++;
}
tok=strtok(NULL,",");
}
Thanks for your input and your help. (Maybe I should have given this a more careful thought before posting.)