Q:

C Program to Delete a Substring From a String [Updated]

belongs to collection: String in C Programs

0

Write a C Program to Delete a Substring From a String or how to remove a word from a string or c program to delete a word from a string. In this program, we have to remove a particular word or substring from a sentence or string. So first we have to take a string and store in a variable after that we have to enter the word or substring which user want to remove from the original string or sentence.


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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#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);
}

 

Output:

Enter Any String to Remove a Word from String: nerdutella example in c pror r graming

 Enter Any Word You Want to be Removed: example

 After Removing Word From String: nerdtella  in c programing

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C Program to Convert String to Integer Without Usi... >>
<< C Program To Delete (Remove) Vowels From A String ...