C Programming: Find the number of times the word 'the' in any combination appears
C String: Exercise-16 with Solution
Write a program in C to find the number of timesa given word 'the' appears in the given string.
Sample Solution:
C Code:
#include <stdio.h>
#include <string.h>
int main() {
int ctr = 0, i, freq = 0; // Declare variables for counting, iteration, and frequency
int t, h, e, spc; // Variables to check for 'the ' pattern
char str[100]; // Declare a character array to store the string
printf("\n\nFind the number of times the word 'the ' in any combination appears :\n"); // Display information about the task
printf("----------------------------------------------------------------------\n");
printf("Input the string : ");
fgets(str, sizeof str, stdin); // Read a string from the standard input (keyboard)
ctr = strlen(str); // Calculate the length of the string
/* Counts the frequency of the word 'the' with a trailing space */
for (i = 0; i <= ctr - 3; i++) {
// Check if the characters form 'the ' pattern (regardless of case)
t = (str[i] == 't' || str[i] == 'T');
h = (str[i + 1] == 'h' || str[i + 1] == 'H');
e = (str[i + 2] == 'e' || str[i + 2] == 'E');
spc = (str[i + 3] == ' ' || str[i + 3] == '\0');
// Increment frequency if 'the ' pattern is found
if ((t && h && e && spc) == 1)
freq++;
}
printf("The frequency of the word \'the\' is : %d\n\n", freq); // Display the frequency of 'the '
return 0; // Return 0 to indicate successful execution of the program
}
Sample Output:
Find the number of times the word 'the ' in any combination appears : ---------------------------------------------------------------------- Input the string : The stering where the word the present more then onces. The frequency of the word 'the' is : 3
Flowchart :
C Programming Code Editor:
Improve this sample solution and post your code through Disqus.
Previous: Write a program in C to read a sentence and replace lowercase characters by uppercase and vice-versa.
Next: Write a program in C to remove characters in String Except Alphabets.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/c-programming-exercises/string/c-string-exercise-16.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics