Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
Old Jun 29th, 2005, 3: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
Old Jun 29th, 2005, 7:11 PM   #2
L7Sqr
Hobbyist Programmer
 
Join Date: Jun 2005
Location: here
Posts: 152
Rep Power: 4 L7Sqr is on a distinguished road
Quote:
BUGS
Never use this function. This function modifies its first
argument. The identity of the delimiting character is
lost. This function cannot be used on constant strings.
From the man pages of strtok.
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]
L7Sqr is offline   Reply With Quote
Old Jun 29th, 2005, 9:39 PM   #3
Ancient Dragon
PFO God In Training
 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 651
Rep Power: 4 Ancient Dragon is on a distinguished road
Quote:
Originally Posted by conbrio
I was sort of surprised there wasn't a function in C's libraries to deal with this..
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;

}
Ancient Dragon is offline   Reply With Quote
Old Jun 30th, 2005, 1:48 PM   #4
conbrio
Newbie
 
Join Date: Apr 2005
Posts: 10
Rep Power: 0 conbrio is on a distinguished road
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.
conbrio is offline   Reply With Quote
Old Jun 30th, 2005, 2:57 PM   #5
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
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
Old Jul 1st, 2005, 6:51 PM   #7
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Jul 1st, 2005, 7:51 PM   #8
Ancient Dragon
PFO God In Training
 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 651
Rep Power: 4 Ancient Dragon is on a distinguished road
Quote:
Originally Posted by Nuticulus
Wait a moment, wasn't it you who made the thread about using pre-incrementation as opposed to post incrementation? Just nitpicking
No -- I doubt it. I have argued that pre and post increment are nearly, if not exactly, the same when NOT used in comparisons, such as when all you want to do is increment the number. That does NOT apply in the examples I show below, when it does make a big difference. The way I incremented counter in my post #3 is exactly the way I always increment counters.


Quote:
Originally Posted by DaWei
I almost always use the post-increment operator.
So do I. I think pre-increment makes for confusing code, and if there is an error, pre-increments can often be overlooked as the culprit. And it just seems to be a more natural way to do things -- evaluate the integer and then increment it. Example: I would write a loop as in the first example, never like the second.

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;
}
Ancient Dragon is offline   Reply With Quote
Reply

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 3:52 AM.

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