C Program to count number of times a word appears in a string

For a better understanding, we always recommend you to learn the basic topics of C programming listed below:

How to count a word in a String?

In this c program, we need to count the number of times a word appears in a given string. Here we need to find a number of times the word "the" comes in a string. For that, check whether each word is starting with the letter 't' if so, then check the next two letters after 't' is 'h' and 'e' and space is coming; if all the conditions are satisfied, increment the counter by one. Finally, we print the output as the counter number. In this program, 'while loop' is used for execution.

What is the syntax of the while loop?

while (testExpression)
   {
        // codes inside the body of a while
   }

Here 'while loop' evaluates test expression. Suppose the test expression is true, code inside 'while loop' is executed. This process continues until the test expression is false if the test expression is a false 'while loop' is terminated.

ALGORITHM

STEP 1: Include the Header files to use the built-in functions in the C program.

STEP 2: Declare the integer variable 'count, times, t, h, e, space, and set count=0, times=0.

STEP 3: Declare the character Array 'str'.

STEP 4: Read a String from the user using the function gets().

STEP 5: By using a while loop with the condition str[count]!='\0' ,increment count by 1.

STEP 6: Assign i=0.

STEP 7: Using another for loop with the condition 'i<=count-3' do step 8.

STEP 8: Check the str[i]=t or str[i]=T ,if true the variable 't' becomes 1.

STEP 9: Check the str[i+1]=h or str[i+1]=H ,if true the variable h becomes 1.

STEP 10: Check the str[i+2]=e or str[i+2]=E ,if true the variable E becomes 1.

STEP 11: Check the str[i+3]=' ' or str[i+3]='\0' ,if true the variable space becomes 1.

STEP 12: Check if 't' and 'h' and 'e' and space are 1, then increment the variable times by 1.

STEP 13: increment 'i' by 1 and repeat step 7.

STEP 14: Then, Display the frequency of the word 'the' in the sentence as times.

C Source Code

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

void main() {
  int count = 0, i, times = 0, t, h, e, space;
  char str[100];
  puts("Enter a string:");
  gets(str);
  while (str[count] != '\0') {
    count++;
  } /*Finding number of times word 'the'*is coming in the sentence*/
  for (i = 0; i <= count - 3; i++) {
    t = (str[i] == 't' || str[i] == 'T');
    h = (str[i + 1] == 'h' || str[i + 1] == 'H'); /* Checks the word "the" is present in the sentence that user enters */
    e = (str[i + 2] == 'e' || str[i + 2] == 'E');
    space = (str[i + 3] == ' ' || str[i + 3] == '\0');
    if ((t && h && e && space) == 1)
      times++;
  }
  printf("Frequency of the word \'the\' is %d\n", times); /* displays output of the program */

}

                                      

OUTPUT

Enter a string:

The Teacher's day is the birth day of Dr.S.Radhakrishnan
Frequency of the word 'the' is 2