Wednesday 25 June 2014

C++ program to remove a word from given string


Here is a C++ program to remove a word from given string


 SOURCE CODE OUTPUT
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
    char str[100],repst[20];
    int i,j=0,k,i1,lenght1;
    cout<<"Enter String : ";
    cin.getline(str,100);
    cout<<"Enter word to be removed : ";
    cin>>repst;
    lenght1=strlen(repst);
    for(i=0;str[i]!='\0';i++)
    {
        if(str[i]==repst[j])
            j++;
        else if(str[i]!=repst[j])
            j=0;
        if(repst[j]=='\0'&&((str[i+1]<65||str[i+1]>90)&&(str[i+1]<97||str[i+1]>122))&&str[i-lenght1]==' ')
        {
            i1=i-lenght1;
                for(k=i1;str[k]!='\0';k++)
                    str[k]=str[k+lenght1+1];
            i=i1;
            j=0;
        }
    }
    cout<<"After removing : "<<str;
    return 0;
}

Any questions regarding to program please write in comments.

No comments:

Post a Comment