Junior — Senior
Rewriting the character escaping function
livecode
Task condition
This is an implementation of the mark_char function, designed to add a backslash before each occurrence of a specified character in a string. The current version of the string traversal is implemented incorrectly, which may lead to incorrect results. The algorithm needs to be fixed so that it iterates through all characters of the original string and returns a new string where each found character is escaped.
std::string mark_char(const std::string& src, char target) {
if (src.empty()) {
return "";
}
std::string out;
for (char ch : src) {
if (ch == target) {
out += '\\';
}
out += ch;
}
return out;
}