C Program to Count Repeated Words from a String

This C Program counts the number of all repeated words in a string and displays the repeated words with it’s frequency.

Here is a source code of the C program that counts the number of all repeated words in a string display the word with it’s frequency. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Count the Number of all Repeated Words
  3.  * in a String & Display the Word with its Frequency
  4.  */
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <ctype.h>
  8.  
  9. struct detail
  10. {
  11.     char word[20];
  12.     int freq;
  13. };
  14.  
  15. int update(struct detail [], const char [], int);
  16.  
  17. int main()
  18. {
  19.     struct detail s[10];
  20.     char string[100], unit[20], c;
  21.     int i = 0, freq = 0, j = 0, count = 0, num = 0;
  22.  
  23.     for (i = 0; i < 10; i++)
  24.     {
  25.        s[i].freq = 0;
  26.     }
  27.     printf("Enter string: ");
  28.     i = 0;
  29.     do
  30.     {
  31.         fflush(stdin);
  32.         c = getchar();
  33.         string[i++] = c;
  34.  
  35.     } while (c != '\n');
  36.     string[i - 1] = '\0';
  37.     printf("The string entered is: %s\n", string);
  38.     for (i = 0; i < strlen(string); i++)
  39.     {
  40.         while (i < strlen(string) && string[i] != ' ' && isalnum(string[i]))
  41.         {
  42.             unit[j++] = string[i++];
  43.         }
  44.         if (j != 0)
  45.         {
  46.             unit[j] = '\0';
  47.             count = update(s, unit, count);
  48.             j = 0;
  49.         }
  50.     }
  51.  
  52.     printf("*****************\nWord\tFrequency\n*****************\n");
  53.     for (i = 0; i < count; i++)
  54.     {
  55.         printf("%s\t   %d\n", s[i].word, s[i].freq);
  56.         if (s[i].freq > 1)
  57.         {
  58.             num++;
  59.         }
  60.     }
  61.     printf("The number of repeated words are %d.\n", num);
  62.  
  63.     return 0;
  64. }
  65.  
  66. int update(struct detail s[], const char unit[], int count)
  67. {
  68.     int i;
  69.  
  70.     for (i = 0; i < count; i++)
  71.     {
  72.         if (strcmp(s[i].word, unit) == 0)
  73.         {
  74.             s[i].freq++;
  75.  
  76.             return count;
  77.         }
  78.     }
  79.     /*If control reaches here, it means no match found in struct*/
  80.     strcpy(s[count].word, unit);
  81.     s[count].freq++;
  82.  
  83.     /*count represents the number of fields updated in array s*/
  84.     return (count + 1);
  85. }

$ cc wordfrequency.c
$ ./a.out
Enter string: hello world hello program hello C 
The string entered is: hello world hello program hello C 
*****************
Word	Frequency
*****************
hello	   3
world	   1
program	   1
C	   1
The number of repeated words are 1.

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement

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.