View Single Post
Old Jun 29th, 2005, 2:55 PM   #1
conbrio
Newbie
 
Join Date: Apr 2005
Posts: 10
Rep Power: 0 conbrio is on a distinguished road
Function that returns occurances of substring in string.

I was sort of surprised there wasn't a function in C's libraries to deal with this..so I got to making it.

int strcnt(char cstr[], char cdlm[]) {
  int oc=0;
  for (strtok(cstr, cdlm); strtok(NULL, cdlm); oc++);
  return oc;
}

This would be fine and well, but I need strtok() to work with the same string (ie, the string fed into the first arg in strcnt) elsewhere in the program, in another function. When I try to use strtok() in a different function with the same string, the second time I call it (to get the second token split off by the delimiter) it returns <null>. So I'm guessing I need to somehow "reset" strtok() at the end of strcnt(). Is this possible?
conbrio is offline   Reply With Quote