browse: forums | FAQ
Connecting Tech Pros Worldwide

Hey there! Do you need C / C++ help?

Get answers from our community of C / C++ experts on BYTES! It's free.

count the number of occurrences of a substring in a string

sibingpeter@gmail.com
Guest
 
Posts: n/a
#1: Nov 15 '05
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?




Charles Mills
Guest
 
Posts: n/a
#2: Nov 15 '05

re: count the number of occurrences of a substring in a string



sibingpeter@gmail.com wrote:[color=blue]
> 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?[/color]

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

Barry
Guest
 
Posts: n/a
#3: Nov 15 '05

re: count the number of occurrences of a substring in a string



"Charles Mills" <cmills@freeshell.org> wrote in message
news:1128141369.782403.206760@g14g2000cwa.googlegr oups.com...[color=blue]
>
> sibingpeter@gmail.com wrote:[color=green]
> > 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?[/color]
>
> 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
>[/color]

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

Barry


sibingpeter@gmail.com
Guest
 
Posts: n/a
#4: Nov 15 '05

re: count the number of occurrences of a substring in a string


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.

Malcolm
Guest
 
Posts: n/a
#5: Nov 15 '05

re: count the number of occurrences of a substring in a string



<sibingpeter@gmail.com> wrote[color=blue]
>
> "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.
>[/color]
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.


Closed Thread