Remove and replace the character from the String coderinme

remove and replace the character from the String

String
#include <iostream>
#include <string>
using namespace std;
class mystring
{
char s[20];
public:
void readstr()
{
cout<<"ENTER THE STRING: ";
cin.getline(s,20);
}
void show()
{
cout<<"\nYOUR ENTERED STRING IS: "<<s;
}
int replace(char c1,char c2)
{
int i=0,count=0;
while(s[i]!='\0')
{
if(s[i]==c1)
{
s[i]=c2;
count++;
}
i++;
}
return count;
}
};
int main()
{
mystring r;
r.readstr();
char p1,p2;
cout<<"ENTER CHARACTER YOU WANT TO REMOVE: ";
cin>>p1;
cout<<"ENTER THE NEW CHARACTER YOU WANT TO REPLACE WITH: ";
cin>>p2;
int b=r.replace(p1,p2);
cout<<"TOTAL NUMBER OF REPLACEMENTS "<<b<<endl;
r.show();
}

output is

ENTER THE STRING: Shadan is a good boy but he is bad in mathematics
CHARACTER YOU WANT TO REMOVE: a
THE NEW CHARACTER YOU WANT TO REPLACE WITH: q
TOTAL NUMBER OF REPLACEMENTS 6

YOUR ENTERED STRING IS: Shqdqn is q good boy but he is bqd in mqthemqtics

you can see

Leave a reply:

Your email address will not be published.