View Single Post
Old Jul 1st, 2005, 3:13 PM   #6
Nuticulus
Programmer
 
Nuticulus's Avatar
 
Join Date: May 2005
Location: England
Posts: 61
Rep Power: 4 Nuticulus is on a distinguished road
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.
Nuticulus is offline   Reply With Quote