First off, since you're using c++, consider using std::string
. Modifying a string literal is undefined behavior (either copy the string literal into a buffer, or use a char []
or std::string
in the first place.)
Second, string literals must be enclosed in double quotes.
Third, you require two sets of backslashes for every backslash you escape. \\\\
turns into \\
.
This should do the trick:
std::string s("C:\\Users\\user\\desktop");
auto it = std::find(s.begin(), s.end(), '\\');
while (it != s.end()) {
auto it2 = s.insert(it, '\\');
// skip over the slashes we just inserted
it = std::find(it2+2, s.end(), '\\');
}
std::cout << s; // C:\\Users\\user\\desktop