C Program to Find the Frequency of “the” Word in a String

This is a C program to find the frequency of the word ‘the’ in a given sentence.

Problem Description

This program takes the sentence as input and finds the frequency of the word ‘the’ in a given sentence.

Problem Solution

1. Take any sentence as input.
2. Check for the word ‘the’ in the input sentence.
3. Use a variable to keep the count of number of ‘the’ in the sentence.

Program/Source Code

Here is source code of the C program to find the frequency of the word ‘the’ in a given sentence. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  *  C program to accept a string and find the number of times the word
  3.  * 'the' appears in that string
  4.  */
  5. #include <stdio.h>
  6.  
  7. void main()
  8. {
  9.     int count = 0, i, times = 0, t, h, e, space;
  10.     char string[100];
  11.  
  12.     puts("Enter a string:");
  13.     gets(string);
  14.    /*   Traverse the string to count the number of characters */
  15.     while (string[count] != '\0')
  16.     {
  17.         count++;
  18.     }
  19.     /*   Finding the frequency of the word 'the' */
  20.     for (i = 0; i <= count - 3; i++)
  21.     {
  22.         t =(string[i] == 't' || string[i] == 'T');
  23.         h =(string[i + 1] == 'h' || string[i + 1] == 'H');
  24.         e =(string[i + 2] == 'e'|| string[i + 2] == 'E');
  25.         space =(string[i + 3] == ' ' || string[i + 3] == '\0');
  26.         if ((t && h && e && space) == 1)
  27.             times++;
  28.     }
  29.     printf("Frequency of the word 'the' is %d\n", times);
  30. }
Program Explanation

1. Take any sentence as input and store it in the array string[].
2. If the input string has ‘t’, ‘h’, ‘e’ and ‘ ‘ consecutively, then store that values in the variables t, h, e and space respectively.
3. Use the variable times to count the number of ‘the ‘ in the input sentence. Increment the variable times if and only if the variables t, h, e and space have values in it.
4. Print the variable times as output and exit.

advertisement
Runtime Test Cases
Enter a string:
The gandhi jayanthi is celeberated on october 2 is the day
that he has born.
Frequency of the word 'the' is 2

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

Free 30-Day Java Certification Bootcamp is Live. Join Now!
If you wish to look at other example programs on Strings, go to C Programming Examples on Strings. If you wish to look at programming examples on all topics, go to C Programming Examples.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 40s–60s and exploring new directions in your career, I also offer mentoring. Learn more here.