C Program to Remove Given Word from a String

This is a C Program to remove given word from a string.

Problem Description

This program takes string and its substring as input and removes the substring from the string.

Problem Solution

1. Take a string and its substring as input.
2. Put each word of the input string into the rows of 2-D array.
3. Search for the substring in the rows of 2-D array.
4. When the substring is got, then override the current row with next row and so on upto the last row.

Program/Source Code

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

  1.  
  2. /*
  3.  * C Program to Remove given Word from a String
  4.  */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8.  
  9. int main ()
  10. {
  11. 	char str[100], word[100], twoD[10][30];
  12. 	int i = 0, j = 0, k = 0, len1 = 0, len2 = 0;
  13.  
  14. 	printf ("Enter the string:\n");
  15. 	gets (str);
  16.  
  17. 	printf ("Enter the word to be removed:\n");
  18. 	gets (word);
  19.  
  20. 	// let us convert the string into 2D array
  21. 	for (i = 0; str[i] != '\0'; i++)
  22. 	{
  23. 		if (str[i] == ' ')
  24. 		{
  25. 			twoD[k][j] = '\0';
  26. 			k ++;
  27. 			j = 0;
  28. 		}
  29. 		else
  30. 		{
  31. 			twoD[k][j] = str[i];
  32. 			j ++;
  33. 		}
  34. 	}
  35.  
  36. 	twoD[k][j] = '\0';
  37.  
  38. 	j = 0;
  39. 	for (i = 0; i < k + 1; i++)
  40. 	{
  41. 		if (strcmp(twoD[i], word) == 0)
  42. 		{
  43. 			twoD[i][j] = '\0';
  44. 		}
  45. 	}
  46.  
  47. 	j = 0;
  48.  
  49. 	for (i = 0; i < k + 1; i++)
  50. 	{
  51. 		if (twoD[i][j] == '\0')
  52. 			continue;
  53. 		else
  54. 			printf ("%s ", twoD[i]);
  55. 	}
  56.  
  57. 	printf ("\n");
  58.  
  59. 	return 0;
  60. }
Program Explanation

1. In this C program, first step is to declare the str, word and twoD arrays. str is used to store string input, word is used to store the input word and twoD is used to store each word in 2D array.
2. Next step is to take the input from the user and also take input word that has to be removed.
3. In for loop statement, traverse until the end of the string and append the character to the n th row until you encounter a white space. If a whitespace is encountered then append \0 to the current row and iterate the steps. By the end of iterations we shall have a 2D array of words. Append \0 to the end of last row.
4. Compare the words that match the given word and make them null by using strcmp function. strcmp function is used to match two strings.
5. Print the words that are not NULL in the twoD array as output and exit.

advertisement
advertisement
Runtime Test Cases

Test case 1 – Here, the entered string is a sentence with some repeated words.

$ gcc remove-word.c -o remove-word
$./remove-word
 
Enter the string:
Hello World hello world Hello W
Enter the word to be removed: Hello
 
World hello world W

Test case 2 – Here, the entered string is a sentence with text and numbers.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
$ gcc remove-word.c -o remove-word
$ ./remove-word
 
Enter the string:
simple world \0\1 0000
Enter the word to be removed: \0\1
 
simple world 0000

Test case 3 – Here, the entered string is a sentence with same repeated words.

$ gcc remove-word.c -o remove-word
$ ./remove-word
 
Enter the string:
Sanfoundry C, Sanfoundry C, Sanfoundry C
Enter the word to be removed: Sanfoundry
 
C, C, C

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
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.