Reputation: 2002
int main ()
{
char str[] ="kk,12,,23,4,,,3434,3,33,,,";
char * valarr;
int count=0;
valarr = strtok(str,",");
while(valarr != '\0')
{
valarr = strtok(NULL,",");
count++;
}
printf("%d\n",count);
return 0;
}
In above program the output is 7.
It seems that the strtok is tokenizing consecutive commas at once.
Instead of consecutive commas I can introduce a blank in between but Is there a way to overcome this so that I have empty space also in the count ?
Upvotes: 2
Views: 1300
Reputation: 399833
Correct. The documentation states this pretty clearly:
A sequence of two or more contiguous delimiter characters in the parsed string is considered to be a single delimiter.
That's just how strtok()
is supposed to work. You might be better of rolling your own, which will also free you from strtok()
's nastiness.
Upvotes: 5
Reputation: 2442
Short answer: NO At least using strtok, check this to learn what's better for your application.
Upvotes: 1