Quote:
|
Originally Posted by Ancient Dragon
Here is another way to count the number of occurrences without using strtok()
int main()
{
char str[] = "HelloHelloHello";
int counter = 0;
char*ptr = strstr(str,"Hello");
while(ptr != 0)
{
counter++;
ptr = strstr(ptr+1,"Hello");
}
printf("counter = %d\n",counter);
return 0;
}
|
Wait a moment, wasn't it you who made the thread about using pre-incrementation as opposed to post incrementation? Just nitpicking
But anyway the code is a perfectly fine substitute for using strtok for that purpose. I never knew it was recommended not to use strtok before. Btw, I read the strsep() manpage and it appears to suffer the same problems as strtok(), i.e. it modifies the string. So it's not exactly a great substitute.