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
char string, word, unit, c;
int i = 0, j = 0, count = 0;
printf("Enter string: ");
i = 0;
c = getchar();
string[i++] = c;
} while (c != '\n');
string[i - 1] = '\0';
printf("Enter the word you want to find: ");
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)
j = 0;
printf("The number of times the word '%s' found in '%s' is '%d'.\n", word, string, count);
$ 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.