PUBLISHED ON: FEBRUARY 17, 2022
C Program to Count the Number of Repeated Occurrences of a particular Word in a String

Logic to Count the Number of Repeated Occurrences of a particular Word in a String:
- Get the input from the user and store it in the array variable,
- By using the for loop statement, the no. of repeated words can be counted,
- Using while loop the special characters of the occurred word can be checked,
- Once the condition results true then, print the word.
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 str[50], m1[30], m2[30], ch; int x = 0, y = 0, z = 0; printf("Enter The String To Find The Repetive Word: "); x = 0; do { fflush(stdin); ch = getchar(); str[x++] = ch; } while (ch != '\n'); str[x - 1] = '\0'; printf("Enter Any Word From The Given String To Search: "); scanf("%s", m1); for (x = 0; x < strlen(str); x++) { while (x < strlen(str) && !isspace(str[x]) && isalnum(str[x])) { m2[y++] = str[x++]; } if (y != 0) { m2[y] = '\0'; if (strcmp(m2, m1) == 0) { z++; } y = 0; } } printf("The Word '%s' From The Given String '%s' is found %d'.\n Times", m1, str, z); }
Output:
