C Program to Delete All Repeated Words in String

This C Program Deletes All Repeated Words in String.

Here is source code of the C Program to Delete All Repeated Words in 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 Delete All Repeated Words in String
  3.  */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7.  
  8. int main ()
  9. {
  10. 	char str[100], word[100], twoD[10][30];
  11. 	int i = 0, j = 0, k = 0, len1 = 0, len2 = 0, l = 0;
  12.  
  13. 	printf ("Enter the string\n");
  14. 	gets (str);
  15.  
  16. 	// let us convert the string into 2D array
  17. 	for (i = 0; str[i] != '\0'; i++)
  18. 	{
  19. 		if (str[i] == ' ')
  20. 		{
  21. 			twoD[k][j] = '\0';
  22. 			k ++;
  23. 			j = 0;
  24. 		}
  25. 		else
  26. 		{
  27. 			twoD[k][j] = str[i];
  28. 			j ++;
  29. 		}
  30. 	}
  31.  
  32. 	twoD[k][j] = '\0';
  33.  
  34. 	j = 0;
  35. 	for (i = 0; i < k; i++)
  36. 	{
  37. 		int present = 0;
  38. 		for (l = 1; l < k + 1; l++)
  39. 		{
  40. 			if (twoD[l][j] == '\0' || l == i)
  41. 			{
  42. 				continue;
  43. 			}
  44.  
  45. 			if (strcmp (twoD[i], twoD[l]) == 0) {
  46. 				twoD[l][j] = '\0';
  47. 				present = present + 1;
  48. 			}
  49. 		}
  50. 		// if (present > 0)	     | uncomment this `if` block if you
  51. 		// {			     | want to remove all the occurrences 
  52. 		// 	twoD[i][j] = '\0';   | of the words including the word
  53. 		// }			     | itself.
  54. 	}
  55.  
  56. 	j = 0;
  57.  
  58. 	for (i = 0; i < k + 1; i++)
  59. 	{
  60. 		if (twoD[i][j] == '\0')
  61. 			continue;
  62. 		else
  63. 			printf ("%s ", twoD[i]);
  64. 	}
  65.  
  66. 	printf ("\n");
  67.  
  68. 	return 0;
  69. }

Enter the string
welcome to sanfoundry's c programming class ,  welcome again to c class !
welcome to sanfoundry's c programming class , again !
 
Enter the string:
Welcome to Sanfoundry C Class, Welcome to Java Programming, Welcome to C++ class
Welcome to Sanfoundry C Class, Java Programming, C++ class

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.

If you find any mistake above, kindly email to [email protected]

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.