Replacement

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Replacement:

#include<iostream>

using namespace std;

class abc

char st[20],st1[10],st2[10],subst1[10],subst2[10];

int loc,len,s,sublen,total;

public:

void setvalue()

cout<<"enter string"<<endl;

cin>>st;

cout<<"enter sub string to be replace"<<endl;

cin>>subst1;

cout<<"enter substring to replace with"<<endl;

cin>>subst2;

void length()

for(int i=0;st[i]!='\0';i++)

len=i;

for(int i=0;subst1[i]!='\0';i++)

sublen=i;

total=len-sublen+1;

int match()

int i,c;
for(c=0;c<=total;c++)

for(i=0;i<=sublen;i++)

if(i==sublen)

loc=c;

return 0;

if(subst1[i]!=st[c+i])

break;

cout<<"pattern not matched"<<endl;

return 0;

void extract()

for(int i=0;i<=loc-1;i++)

st1[i]=st[i];

st1[i]='\0';

sublen=sublen+i;

for(int j=sublen;st[j]!='\0';j++)

st2[j-sublen]=st[j];

st2[j-sublen]='\0';

}
}

void replace()

int i,c;

for(i=0,c=0;st1[i]!='\0';i++,c++)

st[c]=st1[i];

for(i=0;subst2[i]!='\0';i++,c++)

st[c]=subst2[i];

for(i=0;st2[i]!='\0';i++,c++)

st[c]=st2[i];

st[c]='\0';

cout<<"resultant string = "<<st<<endl;

};

int main()

abc a1;

a1.setvalue();

a1.length();

a1.match();

a1.extract();

a1.replace();

You might also like