count the number of occurrences of a substring in a string

Discussion in 'C Programming' started by sibingpeter@gmail.com, Oct 1, 2005.

  1. Guest

    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?
     
    , Oct 1, 2005
    #1
    1. Advertising

  2. 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
     
    Charles Mills, Oct 1, 2005
    #2
    1. Advertising

  3. Barry Guest

    "Charles Mills" <> wrote in message
    news:...
    >
    > 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
     
    Barry, Oct 1, 2005
    #3
  4. Guest

    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.
     
    , Oct 1, 2005
    #4
  5. Malcolm Guest

    <> 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.
     
    Malcolm, Oct 2, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. yogi_bear_79
    Replies:
    9
    Views:
    5,020
    James Kanze
    Feb 28, 2008
  2. C3
    Replies:
    3
    Views:
    129
    Brian McCauley
    Sep 29, 2004
  3. Replies:
    3
    Views:
    213
    Sherm Pendley
    Aug 3, 2005
  4. PerlFAQ Server
    Replies:
    0
    Views:
    179
    PerlFAQ Server
    Jan 4, 2011
  5. PerlFAQ Server
    Replies:
    0
    Views:
    161
    PerlFAQ Server
    Apr 22, 2011
Loading...

Share This Page