string replace is not working for "\'"(\+Apostrophe)

Currently I am trying to write a program where my requirement is I want to change ‘ as \’ using C++.

I used existing C++ String:replace class and it is not give any output.

#include <iostream>
#include <string>
using namespace std;

int main()
char memory[ ] = "This is the's char's array's.";

std::string s(memory);

std::string FindWord = "\'";
std::string ReplaceWord = "\\'";
std::size_t index;
while ((index = s.find(FindWord)) != std::string::npos)
s.replace(index, FindWord.length(), ReplaceWord);


Actual Output:
It is crashing.

Expected Output:
This is the\’s char\’s array\’s (\+Apostrophe)

Instead of "\\'" (’)if I use any other char it is replacing correctly, but when I use ‘ it is treated as escape sequence and it fails.

Anybody helping me to fix this problem is appreciated.

can you use code tags please.

stick a breakpoint at the end of the while loop, and go through it 3 times say..

this is what 's' looks like on my box at that point in time:

"This is the\\\'s char's array's."

does that give you a hint?
Last edited on
you also need to escape the \ ... like \\ and \'
std::string ReplaceWord = "\\\'";

sorry mutexe, took the wind from your sails :)
Last edited on
No worries dude :)
Topic archived. No new replies allowed.