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.
/*
* C Program to Count the Number of Unique Words
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
int i = 0, e, j, d, k, space = 0;
char a[50], b[15][20], c[15][20];
printf("Read a string:\n");
fflush(stdin);
scanf("%[^\n]s", a);
for (i = 0;a[i] != '\0';i++) //loop to count no of words
{
if (a[i] = = ' ')
space++;
}
i = 0;
for (j = 0;j<(space + 1);i++, j++) //loop to store each word into an 2D array
{
k = 0;
while (a[i] != '\0')
{
if (a[i] == ' ')
{
break;
}
else
{
b[j][k++] = a[i];
i++;
}
}
b[j][k] = '\0';
}
i = 0;
strcpy(c[i], b[i]);
for (e = 1;e <= j;e++) //loop to check whether the string is already present in the 2D array or not
{
for (d = 0;d <= i;d++)
{
if (strcmp(c[i], b[e]) == 0)
break;
else
{
i++;
strcpy(c[i], b[e]);
break;
}
}
}
printf("\nNumber of unique words in %s are:%d", a, i);
return 0;
}
$ 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.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
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.
Next Steps:
- Get Free Certificate of Merit in C Programming
- Participate in C Programming Certification Contest
- Become a Top Ranker in C Programming
- Take C Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Related Posts:
- Buy C Books
- Practice Computer Science MCQs
- Watch Advanced C Programming Videos
- Practice BCA MCQs
- Apply for Computer Science Internship