codescracker
c++

C++ Program to Delete a Word from a String



« Previous Program Next Program »


In this article, you will learn and get code to delete (or remove) a word from a string in C++. Both word and string must be entered by user at run-time.

Here are the list of programs, you will go through:

Note - The first program has a limitation, that if user enters the string as this is codescracker and the word (to delete) as is. Then is from this also gets deleted. So the new string will be th codescracker. To overcome this problem, we've modified this program.

Second program (modified program) also removes extra spaces from the new string.

Delete a Word from a String

To delete any desired word from a given string in C++ programming, you have to ask from user to enter the string and word. Then delete the entered word (along with its duplicate) from the entered string.

The question is, write a program in C++ that deletes a word from a string. Here is its answer:

// C++ Program to Delete a Word from a String
// ----codescracker.com----

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int main()
{
    char str[200], wrd[20];
    int i, j, strLen, wrdLen, tmp, chk=0;
    cout<<"Enter the String: ";
    gets(str);
    cout<<"Enter the Word: ";
    cin>>wrd;
    strLen = strlen(str);
    wrdLen = strlen(wrd);
    for(i=0; i<strLen; i++)
    {
        tmp = i;
        for(j=0; j<wrdLen; j++)
        {
            if(str[i]==wrd[j])
                i++;
        }
        chk = i-tmp;
        if(chk==wrdLen)
        {
            i = tmp;
            for(j=i; j<(strLen-wrdLen); j++)
                str[j] = str[j+wrdLen];
            strLen = strLen-wrdLen;
            str[j]='\0';
        }
    }
    cout<<"\nNew String = "<<str;
    cout<<endl;
    return 0;
}

This program was build and run under Code::Blocks IDE. Here is its sample run:

C++ program to delete word from string sentence

Now supply any string say Hello from C++, Hello from codescracker and a word say Hello. Press ENTER key to delete Hello from the entered string as shown here in the snapshot given below:

delete word from string c++

The dry run of above program with user input as provided in above sample run, goes like:

Note - If you want to create the same program, without using string function, strlen(), then you can refer to Count Occurrence of a Word in a String. I know the program is not same, but that article is little similar to this one. There we've counted the desired word from string without using any string function. So you can refer that article as an idea, to implement with yourself. That will be more helpful for you.

Modified Version of Previous Program

Here is the complete version of previous program that deletes a word from a string. This program also removes extra spaces from the new string.

// Modified (Complete) Version of Previous Program
// ----codescracker.com----

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int main()
{
    char str[200], wrd[20];
    int i, j, strLen, wrdLen, tmp, chk=0;
	int doIncrement, isSpace;
    cout<<"Enter the String: ";
    gets(str);
    cout<<"Enter the Word: ";
    cin>>wrd;
    strLen = strlen(str);
    wrdLen = strlen(wrd);
    for(i=0; i<strLen; i++)
    {
        tmp = i;
        doIncrement = 0;
        for(j=0; j<wrdLen; j++)
        {
            if(str[i]==wrd[j])
            {
                if(tmp>0 && (tmp+wrdLen)<strLen)
                {
                    if(str[tmp-1]== ' ' && str[tmp+wrdLen]==' ')
                        doIncrement=1;
                }
                else if(tmp==0 && (tmp+wrdLen)<strLen)
                {
                    if(str[tmp+wrdLen]==' ')
                        doIncrement=1;
                }
                else if(tmp>0 && (tmp+wrdLen)==strLen)
                {
                    if(str[tmp-1]== ' ')
                        doIncrement=1;
                }
                if(doIncrement==1)
                    i++;
                else
                    break;
            }
        }
        chk = i-tmp;
        if(chk==wrdLen)
        {
            i = tmp;
            for(j=i; j<(strLen-wrdLen); j++)
                str[j] = str[j+wrdLen];
            strLen = strLen-wrdLen;
            i = tmp;
            str[j]='\0';
        }
    }
    strLen = strlen(str);
    i=0;
    while(str[i]!='\0')
    {
        isSpace = 0;
        if(str[i]==' ' && str[i+1]==' ')
        {
            for(j=i; j<(strLen-1); j++)
            {
                str[j] = str[j+1];
                isSpace = 1;
            }
        }
        if(i==0 && str[i]==' ')
        {
            for(j=i; j<(strLen-1); j++)
            {
                str[j] = str[j+1];
                isSpace = 1;
            }
        }
        if(isSpace==0)
            i++;
        else
        {
            str[j]='\0';
            strLen--;
        }
    }
    cout<<"\nNew String = "<<str;
    cout<<endl;
    return 0;
}

Here is its sample run with user input, this is is is isth codescracker as string and is as the word to delete:

remove word from string c++

Using Two dimensional Array

Using two-dimensional array, the program becomes more easier to create or understand. Here is the program:

// Same Program using Two Dimensional Array
// ----codescracker.com----

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int main()
{
    char str[400], wrd[20], strInTwoDArray[20][20];
    int i, j=0, k=0;
    printf("Enter the String: ");
    gets(str);
    printf("Enter the Word (to be Delete): ");
    gets(wrd);
    for(i=0; str[i]!='\0'; i++)
    {
        if(str[i]==' ')
        {
            strInTwoDArray[k][j]='\0';
            k++;
            j=0;
        }
        else
        {
            strInTwoDArray[k][j]=str[i];
            j++;
        }
    }
    strInTwoDArray[k][j] = '\0';
    j=0;
    for(i=0; i<(k+1); i++)
    {
        if(!strcmp(strInTwoDArray[i], wrd))
            strInTwoDArray[i][j]='\0';
    }
    printf("\nThe New String is: ");
    j=0;
    for(i=0; i<(k+1); i++)
    {
        if(strInTwoDArray[i][j] == '\0')
            continue;
        else
            printf("%s ", strInTwoDArray[i]);
    }
    cout<<endl;
    return 0;
}

Here is its sample run with user input, is is is this is isth this is (as string) and is (as word):

delete word from string using two array c++

Same Program in Other Languages


« Previous Program Next Program »