Hello,
I am trying to find and replace a character in a string.
Code:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s("This is a piece of text. All of the \"a's\" will be replaced by \"XXX's\".");
const char target = 'a';
string replace_with("XXX");
size_t pos = 0;
while(pos != string::npos)
{
pos = s.find(target, pos, 1);
if(pos != string::npos)
{
s.replace(pos, 1, replace_with);
pos++;
}
}
cout << s << endl;
return 0;
}
I am getting an error on the line that defines target.
error: invalid conversion from ‘char’ to ‘const char*’
If I put "a", in find(), in place of target it works.