C Program to Find the Frequency of Every Word in a given String

«
»
This C Program finds frequency of every word in a given string.

Here is source code of the C Program to find the frequency of every word in a given string. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /* 
  2.  * C Program to Find the Frequency of  Every Word in a 
  3.  * given String
  4.  */
  5. #include <stdio.h>
  6. #include <string.h>
  7.  
  8. void main()
  9. {
  10.     int count = 0, c = 0, i, j = 0, k, space = 0;
  11.  
  12.     char str[100], p[50][100], str1[20], ptr1[50][100];
  13.  
  14.     char *ptr;
  15.  
  16.     printf("Enter the string\n");
  17.     scanf(" %[^\n]s", str);
  18.  
  19.     printf("string length is %d\n", strlen(str));
  20.  
  21.     for (i = 0;i<strlen(str);i++)
  22.     {
  23.         if ((str[i] == ' ')||(str[i] == ',' && str[i+1] == ' ')||(str[i] == '.'))
  24.         {
  25.             space++;
  26.         }
  27.     }
  28.  
  29.     for (i = 0, j = 0, k = 0;j < strlen(str);j++)
  30.     {
  31.         if ((str[j] == ' ')||(str[j] == 44)||(str[j] == 46))  
  32.         {    
  33.             p[i][k] = '\0';
  34.             i++;
  35.             k = 0;
  36.         }        
  37.         else
  38.              p[i][k++] = str[j];
  39.     }
  40.  
  41.     k = 0;
  42.  
  43.     for (i = 0;i <= space;i++)
  44.     {
  45.         for (j = 0;j <= space;j++)
  46.         {
  47.             if (i == j)
  48.             {
  49.                 strcpy(ptr1[k], p[i]);
  50.                 k++;
  51.                 count++;
  52.  
  53.                 break;
  54.             }
  55.             else
  56.             {
  57.                 if (strcmp(ptr1[j], p[i]) != 0)
  58.                     continue;
  59.                 else
  60.                     break;
  61.             }
  62.         }
  63.     }
  64.  
  65.     for (i = 0;i < count;i++) 
  66.     {
  67.         for (j = 0;j <= space;j++)
  68.         {
  69.             if (strcmp(ptr1[i], p[j]) == 0)
  70.                 c++;
  71.         }
  72.         printf("%s -> %d times\n", ptr1[i], c);
  73.         c = 0;
  74.     }
  75. }

advertisement
Enter the string:
Welcome to Sanfoundry Welcome to C Class
string length is 40
Welcome -> 2 times
to -> 2 times
Sanfoundry -> 1 times
C -> 1 times
Class -> 1 times
 
Enter the string:
Welcome to C Class, Java Class
string length is 30
Welcome -> 1 times
to -> 1 times
C -> 1 times
Class -> 2 times
 -> 1 times
Java -> 1 times

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Note: Join free Sanfoundry classes at Telegram or Youtube
advertisement
advertisement

Here’s the list of Best Reference 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
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 & technical discussions at Telegram SanfoundryClasses.