Below we have explained everything about the program to remove substring from a string, Deleting Substring in C is not very hard it's very simple so don't worry about the solution.
Explanation How to Remove a Word from a String
C Program to Delete a Substring From a String: Just like a searching an element in a sentence or in the string we have to perform the same operation to delete the particular word in a string or sentence. After that, we have to first find the word is present in the string on sentence after that remove the word from the string or sentence.
C Program to Delete a Word from a String: So first run a loop for the main string and compare the string to substring, for each iteration increase the value of the index if the substring is matched with the main string. Then remove or if you want to print only the output so you can skip the character from main strings.
C Program to Delete a Substring From a String
#include<stdio.h>
#include<string.h>
int main()
{
int i, j = 0, k = 0,n = 0;
int flag = 0;
char str[100], neww[100], word[100];
printf("Enter Any String to Remove a Word from String: ");
gets(str);
printf("\n\n Enter Any Word You Want to be Removed: ");
gets(word);
for(i = 0 ; str[i] != '\0' ; i++)
{
k = i;
while(str[i] == word[j])
{
i++,j++;
if(j == strlen(word))
{
flag = 1;
break;
}
}
j = 0;
if(flag == 0)
i = k;
else
flag = 0;
neww[n++] = str[i];
}
neww[n] = '\0';
printf("\n\n After Removing Word From String: %s",neww);
}
Delete a Substring From a String Program Output
Similar to C Program to Delete a Word from a String
- C Program to Compare Two Strings Without Using strcmp Function
- C Program to Convert String to Integer Without Using Library Function atoi()
- C Program to Convert String Lowercase to Uppercase And Vice Versa
- Program to Reverse a String in C | Using Loop, Recursion, and Strrev()
- C Program For Reverse a String Using Pointers
- Program To Compare Two String Using Pointer
- C Program to Compare Two Strings Using STRCMP
- Program to Check String is a Palindrome or Not in C
- C Program to Count Vowels, Consonants, Digits and Spaces in Given String
- C Program to Reverse The Words in a Sentence
I hope now you got the solution for How to Remove a Word from a String. Please Like and share.
0 Comments: