C Program to Count the Occurrences of a Word in a String

This article will teach you and provide you with code to count the occurrences of a word in a string using the C programming language. The user must enter the word and string here (at run-time). For example, if the given string is:

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

then the occurrence of the word

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

Counting Words in a String

To count the occurrence of a word in a string or sentence in C programming, you have to ask the user to enter the string and a word, and then count 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 in the Code::Blocks IDE. Here is its sample run after a 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 how many times this word occurred in the given string:

c count occurrence of word in string

Program Explained

This program has a limitation: if you enter a string, such as "this is codescracker," and a word, such as "is," the occurrence will be displayed as 2. Because "is" from "this" will be counted as well. To overcome this problem, let me create another program for you.

Complete Version of the Counting a Word in a String Program

Here is the complete version of the 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

The same program in different languages

C Quiz


« Previous Program Next Program »


Liked this post? Share it!