This is a C program to find the frequency of the word ‘the’ in a given sentence.
This program takes the sentence as input and finds the frequency of the word ‘the’ in a given sentence.
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.
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.
/*
* C program to accept a string and find the number of times the word
* 'the' appears in that string
*/
#include <stdio.h>
void main()
{
int count = 0, i, times = 0, t, h, e, space;
char string[100];
puts("Enter a string:");
gets(string);
/* Traverse the string to count the number of characters */
while (string[count] != '\0')
{
count++;
}
/* Finding the frequency of the word 'the' */
for (i = 0; i <= count - 3; i++)
{
t =(string[i] == 't' || string[i] == 'T');
h =(string[i + 1] == 'h' || string[i + 1] == 'H');
e =(string[i + 2] == 'e'|| string[i + 2] == 'E');
space =(string[i + 3] == ' ' || string[i + 3] == '\0');
if ((t && h && e && space) == 1)
times++;
}
printf("Frequency of the word 'the' is %d\n", times);
}
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.
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
- Get Free Certificate of Merit in C Programming
- Participate in C Programming Certification Contest
- Become a Top Ranker in C Programming
- Take C Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Practice Computer Science MCQs
- Watch Advanced C Programming Videos
- Buy Computer Science Books
- Buy C Books
- Apply for C Internship