Search code examples
csubstringc-stringscountingfunction-definition

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;
}

Solution

  • 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.