Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Counting the number of occurences of a specific word within a string

The problem with this function is that it looks after all the substrings, but not for words like for example if I'm looking for "hi" within "hifive to to evereyone" it returns 1

int HowManyString(char *satz,char *word) {
    int coun = 0;
    while (strlen(word)<strlen(satz)) {
        if (strstr(satz,word)==NULL) {
            return coun;
        } else {
            satz=strstr(satz,word)+strlen(word);
            if(*(satz)==' '||*(satz)=='\0'){
                coun++;
            } else {
                return coun;
            }
        }
    }
    return coun;
}
like image 600
Muhito Avatar asked Dec 07 '25 07:12

Muhito


2 Answers

Using your approach with the standard function strstr your function can look the following way as shown in the demonstration program below

#include <stdio.h>
#include <string.h>
#include <ctype.h>

size_t HowManyString( const char *s1, const char *s2 )
{
    size_t count = 0;

    size_t n = strlen( s2 );

    for ( const char *p = s1; ( p = strstr( p, s2 ) ) != NULL; p += n )
    {
        if ( ( p == s1 || isblank( ( unsigned char )p[-1] ) ) &&
             ( p[n] == '\0' || isblank( ( unsigned char )p[n] ) ) )
        {
            ++count;
        }            
    }

    return count;
}

int main( void )
{
    const char *s1 = "hifive";
    const char *s2 = "hi";

    printf( "%zu\n", HowManyString( s1, s2 ) );

    s1 = "fivehi";

    printf( "%zu\n", HowManyString( s1, s2 ) );

    s1 = "hi five";

    printf( "%zu\n", HowManyString( s1, s2 ) );

    s1 = "five hi";

    printf( "%zu\n", HowManyString( s1, s2 ) );
}

The program output is

0
0
1
1

If the source string can contain the new line character '\n' when within the function use isspace instead of isblank.

like image 115
Vlad from Moscow Avatar answered Dec 08 '25 20:12

Vlad from Moscow


Here is a function that achieves what you are looking for:

int count_words(const char *sentence, const char *word)
{
    int counter = 0;
    
    for (const char *p = sentence; *p; ++p) {
        // Skip whitespaces
        if (isspace(*p))
            continue;
        
        // Attempt to find a match
        const char *wp = word, *sp = p;
        while (*wp != '\0' && *sp != '\0' && *wp == *sp) {
            ++wp;
            ++sp;
        }
        
        // Match found AND a space after AND a space before
        if (*wp == '\0' && (isspace(*sp) || *sp == '\0') && (p == sentence || isspace(*(p-1)))) {
            ++counter;
            p = sp - 1;
        }
        
        // End of sentence reached: no need to continue.
        if (*sp == '\0')
            return counter;
    }
    
    return counter;
}

Usage:

int main(void)
{
    const char sentence[] = "I is Craig whoz not me, not him, not you!";
    const char word[] = "not";
    
    int occ = count_words(sentence, word);
    
    printf("There are %d occurences.\n", occ);
}

Output:

There are 3 occurences.
like image 26
Zakk Avatar answered Dec 08 '25 19:12

Zakk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!