C Program to Count Particular Repeated Word in a String

This is a C Program to count the number of repeated occurrences of a particular word in a string.

Problem Description

This C Program counts the number of repeated occurrences of a particular word in a string.

Problem Solution

Take input from the user and performs string operations to count the repeated occurrences of a particular word as shown in the program below.

Program/Source Code

Here is a source code of the C program that counts the number of repeated occurrences of a particular word in a string. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

/*
 * C Program to Count the Number of Repeated Occurrences
 * of a particular Word in a String
 */
#include <stdio.h>
#include <string.h>
#include <ctype.h>
 
int main()
{
    char string[100], word[20], unit[20], c;
    int i = 0, j = 0, count = 0;
 
    printf("Enter string: ");
    i = 0;
    do
    {
        fflush(stdin);
        c = getchar();
        string[i++] = c;
 
    } while (c != '\n');
    string[i - 1] = '\0';
    printf("Enter the word you want to find: ");
    scanf("%s", word);
    for (i = 0; i < strlen(string); i++)
    {
        while (i < strlen(string) && !isspace(string[i]) && isalnum(string[i]))
        {
            unit[j++] = string[i++];
        }
        if (j != 0)
        {
            unit[j] = '\0';
            if (strcmp(unit, word) == 0)
            {
                count++;
            }
            j = 0;
        }
    }
 
    printf("The number of times the word '%s' found in '%s' is '%d'.\n", word, string, count);
}
Program Explanation

In this C Program, we are reading the word to find using the ‘word’ variable. For loop is used to count the number of repeated occurrences of a particular word in a string.

advertisement
advertisement

While loop is used to check that the length, space, and alphanumeric number string is less than the occurring string. If the condition is true, then execute the iteration of the loop. If condition statement is used to check that the value of ‘j’ variable is not equal to 0.

If the condition is true then execute the statement, if statement is used to check that the string compared is equal to 0. If the condition is true then execute the statement. Print number of repeated occurrences of a particular word in a string.

Runtime Test Cases
 
$ cc givenword.c 
$ ./a.out
Enter string: hello world hello program hello C
Enter the word you want to find: hello
The number of times the word 'hello' found in 'hello world hello program hello C' is 3.

Sanfoundry Global Education & Learning Series – 1000 C Programs.

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

If you wish to look at programming examples on all topics, go to C Programming Examples.

advertisement
If you find any mistake above, kindly email to [email protected]

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
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.