Wednesday 25 June 2014

C++ program to remove sub string from given string


Here is a C++ program to remove sub string 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 sub string 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')
        {
            i1=i-lenght1+1;
                for(k=i1;str[k]!='\0';k++)
                    str[k]=str[k+lenght1];
            i=i1;
            j=0;
        }
    }
    cout<<"After removing : "<<str;
    return 0;
}

Any questions regarding to program please write in comments.

No comments:

Post a Comment