could something like this work? I've looked at guides and they don't seem to work for replacing a single char with two or more chars.
for (int i = 0; i < s.length(); i++){
if(s[i] == '\"')
s[i] = '\"\"';
}
cout << s;
could something like this work? I've looked at guides and they don't seem to work for replacing a single char with two or more chars.
for (int i = 0; i < s.length(); i++){
if(s[i] == '\"')
s[i] = '\"\"';
}
cout << s;
Your code will not perform as you expect.
You are telling std::string
to assign a 2 character constant, '\"\"'
, into a slot that holds a single character.
Search the basic_string
section of your favorite C++ reference to find a method to insert a string or multiple characters into a string.
Edit 1 - Example
std::string::size_type position = 0;
position = s.find('"');
while (position != std::string::npos)
{
s.insert(position, "\"");
position += 2;
position = s.find('"');
}