C Program to Count Occurrence of a Word in a String

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:

  • codescracker is 3
  • It is 3
  • was is 1
  • is is 1
  • will is 1
  • be is 1

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:

#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

  • The two variables, ls and lw is used to hold the length of string and word respectively
  • The first for loop runs from the first character to the last character of string
  • And the second for loop runs from the first character to the last character of given word
  • Inside the second for loop, we have checked whether the given word present inside the string or not, using character-by-character matching
  • That is, if first character of word gets found in the string at any index, then checks for second match. That is, match second character of word with the character just after the previous index, and so on
  • For example, let's suppose the given word is codescracker. So its length is 12. Therefore, if all character continuously gets matched in the given string, then the value of i will be (its previous value + 12).
  • And after exiting from second for loop, using i-temp or (i's previous value+12)-temp gets initialized to chk
  • If previous value of i is 7, then its value will be 7+12 or 19
  • And 19-temp or 19-7 equals 12. So 12 gets initialized to chk
  • Now check whether the value of chk is equal to the length of word or not
  • If it is equal, then the word gets found, so we have to increment the value of countW variable, that holds the occurrence of word
  • Now at last, initialize the value of temp (previous i's value before second for loop) i
  • And continue the process until the last character of the string

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:

#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 »