C Program to Find the Frequency of the Word ‘the’ in a given Sentence

«
»

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.

advertisement
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 Reference Books in C Programming, Data-Structures and Algorithms

advertisement
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
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn