#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
class string
{
char str[10];
int len;
public:
void accept()
{
cout<<"\n\nEnter the string : ";
cin>>str;
len=strlen(str);
}
void disp()
{
cout<<str;
}
void replace(char,char);
};
void string::replace(char c1,char c2)
{
int f=0;
for(int i=0;i<len;i++)
{
if(str[i]==c1)
{
str[i]=c2;
f=1;
}
}
if(f==0)
{
cout<<"\n\nCharacter is not present";
getch();
exit(0);
}

}


int main()
{
char c1,c2;
clrscr();
string a;
a.accept();
cout<<"\n\nEnter the character to replace : ";
cin>>c1;
cout<<"\n\nEnter the character by which u want to replace : ";
cin>>c2;
cout<<"\n\nString before replacement of character : ";
a.disp();
a.replace(c1,c2);
cout<<"\n\nString after replacement of character  : ";
a.disp();
getch();
return 0;
}

Post a Comment

Previous Post Next Post