Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > count the number of occurrences of a substring in a string

Reply
Thread Tools

count the number of occurrences of a substring in a string

 
 
sibingpeter@gmail.com
Guest
Posts: n/a
 
      10-01-2005
Hi there,

Im trying to find the right way to code the loop to count the number of
occurences of a given substring in a string. Im able to find the first
occurence using the strstr function, but I just cant seem to think of
the right loop that would continue searching after finding this first
occurence. Could someone please help me out here?

 
Reply With Quote
 
 
 
 
Charles Mills
Guest
Posts: n/a
 
      10-01-2005

http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Hi there,
>
> Im trying to find the right way to code the loop to count the number of
> occurences of a given substring in a string. Im able to find the first
> occurence using the strstr function, but I just cant seem to think of
> the right loop that would continue searching after finding this first
> occurence. Could someone please help me out here?


I think this is OK:

int
count_strinstr(const char *big, const char *little)
{
const char *p;
int count = 0;
size_t lil_len = strlen(little);

/* you decide what to do here */
if (lil_len == 0) return -1;

p = strstr(big, little);
while (p) {
count++;
p = strstr(p + lil_len, little);
}
return count;
}

If either arg is NULL this will probably barf.

-Charlie

 
Reply With Quote
 
 
 
 
Barry
Guest
Posts: n/a
 
      10-01-2005

"Charles Mills" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
>
> (E-Mail Removed) wrote:
> > Hi there,
> >
> > Im trying to find the right way to code the loop to count the number of
> > occurences of a given substring in a string. Im able to find the first
> > occurence using the strstr function, but I just cant seem to think of
> > the right loop that would continue searching after finding this first
> > occurence. Could someone please help me out here?

>
> I think this is OK:
>
> int
> count_strinstr(const char *big, const char *little)
> {
> const char *p;
> int count = 0;
> size_t lil_len = strlen(little);
>
> /* you decide what to do here */
> if (lil_len == 0) return -1;
>
> p = strstr(big, little);
> while (p) {
> count++;
> p = strstr(p + lil_len, little);
> }
> return count;
> }
>
> If either arg is NULL this will probably barf.
>
> -Charlie
>


It depends on whether the occurrences have to be distinctly separate from
one another.
Consider:
little is "abab"
big is "ababababababab"

Barry


 
Reply With Quote
 
sibingpeter@gmail.com
Guest
Posts: n/a
 
      10-01-2005
Hey Charlie,

"p = strstr(p + lil_len, little);"

That addition of the string length was the logic that I was lookin for.
Thanks a lot for your help. Appreciate it.

 
Reply With Quote
 
Malcolm
Guest
Posts: n/a
 
      10-02-2005

<(E-Mail Removed)> wrote
>
> "p = strstr(p + lil_len, little);"
>
> That addition of the string length was the logic that I was lookin for.
> Thanks a lot for your help. Appreciate it.
>

If you want to allow sustrings to overlap, add one to the pointer. If you
want to disallow this, add the length of the substring, as Charles showed
you.


 
Reply With Quote
 
 
 
Reply

Thread Tools

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
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FAQ 4.29 How can I count the number of occurrences of a substring within a string? PerlFAQ Server Perl Misc 0 04-22-2011 10:00 PM
FAQ 4.29 How can I count the number of occurrences of a substring within a string? PerlFAQ Server Perl Misc 0 01-04-2011 05:00 PM
Beginners: Count occurrences of a string within a string yogi_bear_79 C++ 9 02-28-2008 09:14 PM
RegEx search for a substring within a substring colinhumber@gmail.com Perl Misc 3 08-03-2005 04:29 PM
counting number of occurrences of every possible substring in multiple files C3 Perl Misc 3 09-29-2004 04:53 PM



Advertisments