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

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.

Free 30-Day Python Certification Bootcamp is Live. Join Now!

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
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
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 40s–60s and exploring new directions in your career, I also offer mentoring. Learn more here.