Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

 
 
Thread Tools Display Modes
Prev Previous Post in Thread   Next Post in Thread Next
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
 

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:25 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC