codescracker
c

C Program to Count Occurrence of a Word in a String



« Previous Program Next Program »

In this tutorial, you will learn and get code about how to count occurrence of a word in a string using C language. Here the word and string must be taken by user (at run-time). For example, if the given string is:

It was codescracker. It is codescracker. It will be codescracker

then the occurrence of word:

Now let's move on and implement it in a C program.

Count Word in a String

To count the occurrence of word in a string or sentence in C programming, you have to ask from user to enter the string and a word, to count that how many times the given word is available in the given string as shown in the program given below:

// ----codescracker.com----

#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
    char str[100], word[20];
    int i, j, ls, lw, temp, countW=0, chk;
    printf("Enter the String: ");
    gets(str);
    printf("Enter a Word: ");
    gets(word);
    ls = strlen(str);
    lw = strlen(word);
    for(i=0; i<ls; i++)
    {
        temp = i;
        for(j=0; j<lw; j++)
        {
            if(str[i]==word[j])
                i++;
        }
        chk = i-temp;
        if(chk==lw)
            countW++;
        i = temp;
    }
    printf("\nOccurrence = %d", countW);
    getch();
    return 0;
}

The program was written under Code::Blocks IDE. Here is its sample run after successful build and run:

c program count number of words in sentence

Now supply any string say

It was codescracker. It is codescracker. It will be codescracker

And then enter a word say codescracker to count that how many times this word occurred in the given string:

c count occurrence of word in string

Program Explained

This program has a limitation, that if you enters a string say this is codescracker, and a word say is, then its occurrence will be shown as 2. Because it also gets is from this. To overcome this problem here is another program.

Complete Version of Counting a Word in String Program

Here is the complete version of previous program:

// ----codescracker.com----

#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
    char str[100], word[20];
    int i, j, ls, lw, temp, countW=0, chk, doIncrement;
    printf("Enter the String: ");
    gets(str);
    printf("Enter a Word: ");
    gets(word);
    ls = strlen(str);
    lw = strlen(word);
    for(i=0; i<ls; i++)
    {
        temp = i;
        doIncrement = 0;
        for(j=0; j<lw; j++)
        {
            if(str[i]==word[j])
            {
                if(temp>0 && (temp+lw)<ls)
                {
                    if(str[temp-1]== ' ' && str[temp+lw]==' ')
                        doIncrement=1;
                }
                else if(temp==0 && (temp+lw)<ls)
                {
                    if(str[temp+lw]==' ')
                        doIncrement=1;
                }
                else if(temp>0 && (temp+lw)==ls)
                {
                    if(str[temp-1]== ' ')
                        doIncrement=1;
                }
                if(doIncrement==1)
                    i++;
                else
                    break;
            }
        }
        chk = i-temp;
        if(chk==lw)
            countW++;
        i = temp;
    }
    printf("\nOccurrence = %d", countW);
    getch();
    return 0;
}

Now let's check it out with string input as this is is is isth codescracker, and word input as is:

c count word in string

Same Program in Other Languages

C Online Test


« Previous Program Next Program »



© Copyright 2021. All Rights Reserved.

CodesCracker