C Program to Count Number of Unique Words in a String

This C Program Counts the Number of Unique Words.

Here is source code of the C Program to Count the Number of Unique Words. 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 Unique Words 
  3.  */
  4. #include <stdio.h> 
  5. #include <string.h>
  6. #include <stdlib.h>
  7. int main()
  8. {
  9.     int i = 0, e, j, d, k, space = 0;
  10.     char a[50], b[15][20], c[15][20];
  11.  
  12.     printf("Read a string:\n");
  13.     fflush(stdin);
  14.     scanf("%[^\n]s", a);
  15.     for (i = 0;a[i] != '\0';i++)        //loop to count no of words
  16.     {
  17.         if (a[i] =  = ' ')
  18.             space++;
  19.     }
  20.     i = 0;
  21.     for (j = 0;j<(space + 1);i++, j++)    //loop to store each word into an 2D array
  22.     {
  23.         k = 0;
  24.         while (a[i] != '\0')
  25.         {
  26.             if (a[i] == ' ')
  27.             {
  28.                 break;
  29.             }
  30.             else
  31.             {
  32.                 b[j][k++] = a[i];
  33.                 i++;
  34.             }
  35.         }
  36.         b[j][k] = '\0';
  37.     }
  38.     i = 0;
  39.     strcpy(c[i], b[i]);
  40.     for (e = 1;e <= j;e++)        //loop to check whether the string is already present in the 2D array or not
  41.     {
  42.         for (d = 0;d <= i;d++)
  43.         {
  44.             if (strcmp(c[i], b[e]) == 0)
  45.                 break;
  46.             else
  47.             {
  48.                 i++;
  49.                 strcpy(c[i], b[e]);
  50.                 break;
  51.             }
  52.         }
  53.     }
  54.     printf("\nNumber of unique words in %s are:%d", a, i);
  55.     return 0;
  56. }

$ cc string7.c
$ a.out
Read a string:
Welcome to Sanfoundry's C-programming class,  Welcome again to C class!
The length of input string is:70
 
Number of unique words in Welcome to Sanfoundry's C-programming class,  Welcome again to C class! are:8

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement
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
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 & discussions at Telegram SanfoundryClasses.