![]() |
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[]) {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? |
Quote:
If you want to preserve the string being used, first make a copy and then use the copy to do your dirty work. You could also, if so inclined, write your own version of strtok as well. ;) |
Quote:
:
int main() |
Thanks for alerting me to that L7Sqr. I looked at its man page and noticed that there is apparently something of a replacement for strtok() anyway (strsep()).
Ancient Dragon, that code works fine, thanks. I wasn't aware that if one incremented the pointer strstr() returns, it'd move to the next occurance of the substring.. The man page is kind of cryptic on the matter. :
RETURN VALUEThanks again. |
The pointer you pass represents the beginning of a string. There is no requirement that it be at the beginning of a defined array of char, only that the data from that point onwards qualifies as a string (zero or more char followed by a null terminator).
The pointer returned represents the beginning of the substring. If you increment the pointer from that position, that particular substring can no longer be found; only the characters beyond that position, up to the terminator, are subsequently searched. |
Quote:
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. |
There is a remote chance that a post-increment of a complex object will take a clock cycle or two more than a pre-increment of the same object. For the hugely vast majority of such operations applied to simple or atomic types the code emitted by the compiler will be the same. One would always be safe, I suppose, with the pre-increment. I, however, despise self-anointed high-priest/gurus (let me be clear -- Ancient Dragon is NOT one) that have an endless bag of dictums with which to bury neophytes and enhance their power (e.g., "I said so, that's why"). In the hopes that I may cause one of these types to spit a frothy, bloody, bubble of rage on his/her homemade vestments, I almost always use the post-increment operator.
|
Quote:
Quote:
:
int array[10];:
int array[10]; |
| All times are GMT -5. The time now is 2:34 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC