Hi
How can i replace a character from a introduced string?
let's say i introduce "aabcf"
i want to replace all a leters with b , so i introduce from the keyboard, that i want to replace a with b, what can i do next?
Here is my attempt :
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
string x;
char y, t;
int i;
cout<<"The string is : ";
cin>>x;
cout<< "The desired character is : ";
cin>> x.at(i);
for (i=0; i<x.length(); i++)
{
cin>> x.at(i);
}
cout<< "With what letter do you want to replace it : ";
cin>>y;
for (i=0; i<x.length(); i++)
{
t = x.at(i) == y;
}
cout<<t;
return 0;
}
#include <iostream>
#include <string>
#include <algorithm>
usingnamespace std;
int main()
{
string x;
char y {}, d {};
cout << "The string is: ";
cin >> x;
cout << "The desired character is: ";
cin >> d;
cout << "With what letter do you want to replace it: ";
cin >> y;
std::replace_if(x.begin(), x.end(), [&d](char c) {return d == c; }, y);
cout << x;
}
The string is: aabcf
The desired character is: a
With what letter do you want to replace it: b
bbbcf