![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Apr 2005
Posts: 10
Rep Power: 0
![]() |
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? |
|
|
|
|
|
#2 | |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: here
Posts: 114
Rep Power: 0
![]() |
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. ![]()
__________________
"...and though our kids are blessed their parents let them shoulder all the blame." - The Quiet Things That No One Ever Knows [BrandNew] |
|
|
|
|
|
|
#3 | |
|
PFO God In Training
![]() Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 498
Rep Power: 4
![]() |
Quote:
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;
} |
|
|
|
|
|
|
#4 |
|
Newbie
Join Date: Apr 2005
Posts: 10
Rep Power: 0
![]() |
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 VALUE
The strstr() function returns a pointer to the beginning
of the substring, or NULL if the substring is not found.Thanks again. |
|
|
|
|
|
#5 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#6 | |
|
Programmer
Join Date: May 2005
Location: England
Posts: 61
Rep Power: 4
![]() |
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.
__________________
http://www.nuticulus.net/hackmysig/sig.PNG Give my site a chance, you know you want to ;-) Google will solve 99% of your problems. Including those with your sex life. Caution, may <strike>contain</strike> be nuts. |
|
|
|
|
|
|
#7 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#8 | ||
|
PFO God In Training
![]() Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 498
Rep Power: 4
![]() |
Quote:
Quote:
int array[10];
int i = 0;
while(i < 10)
{
cout << array[i++] << endl;
}int array[10];
int i = -1;
while(i < 10)
{
cout << array[++i] << endl;
} |
||
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|