w3resource

C Programming: Find the number of times the word 'the' in any combination appears


16. Count Word Occurrences

Write a program in C to find the number of timesa given word 'the' appears in the given string.

C Programming: Find the number of times the word 'the' in any combination appears


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
}

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:

Flowchart: Find the number of times the word 'the ' in any combination appears


For more Practice: Solve these Related Problems:

  • Write a C program to count the occurrences of a specific word in a sentence regardless of its case.
  • Write a C program to search for a word in a string and output all starting positions of its occurrences.
  • Write a C program to count overlapping occurrences of a substring within a larger string.
  • Write a C program to manually parse a sentence and count the frequency of each word without using strtok().

Go to:


PREV : Toggle Case of Sentence.
NEXT : Remove Non-Alphabets.

C Programming Code Editor:



Improve this sample solution and post your code through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.