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 ?
Correct. The documentation states this pretty clearly:
That’s just how
strtok()is supposed to work. You might be better of rolling your own, which will also free you fromstrtok()‘s nastiness.