This is a C Program to remove given word from a string.
This program takes string and its substring as input and removes the substring from the string.
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.
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.
/*
* C Program to Remove given Word from a String
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main ()
{
char str[100], word[100], twoD[10][30];
int i = 0, j = 0, k = 0, len1 = 0, len2 = 0;
printf ("Enter the string:\n");
gets (str);
printf ("Enter the word to be removed:\n");
gets (word);
// let us convert the string into 2D array
for (i = 0; str[i] != '\0'; i++)
{
if (str[i] == ' ')
{
twoD[k][j] = '\0';
k ++;
j = 0;
}
else
{
twoD[k][j] = str[i];
j ++;
}
}
twoD[k][j] = '\0';
j = 0;
for (i = 0; i < k + 1; i++)
{
if (strcmp(twoD[i], word) == 0)
{
twoD[i][j] = '\0';
}
}
j = 0;
for (i = 0; i < k + 1; i++)
{
if (twoD[i][j] == '\0')
continue;
else
printf ("%s ", twoD[i]);
}
printf ("\n");
return 0;
}
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.
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.
$ 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.
Here’s the list of Best Books in C Programming, Data-Structures and Algorithms
- Apply for Computer Science Internship
- Apply for C Internship
- Practice Computer Science MCQs
- Check Computer Science Books
- Watch Advanced C Programming Videos