If you want to convert escape characters (\n, \r, etc.) to a literal backslash and the [a-z] character, you can use a switch statement and append to a buffer. Assuming a C++ standard library string, you may do:
std::string escaped(const std::string& input)
{
std::string output;
output.reserve(input.size());
for (const char c: input) {
switch (c) {
case '\a': output += "\\a"; break;
case '\b': output += "\\b"; break;
case '\f': output += "\\f"; break;
case '\n': output += "\\n"; break;
case '\r': output += "\\r"; break;
case '\t': output += "\\t"; break;
case '\v': output += "\\v"; break;
default: output += c; break;
}
}
return output;
}
This uses a switch statement, and converts all common escape sequences to a literal '\' and the character used to represent the escape sequence. All other characters are appended as-is to the string. Simple, efficient, easy-to-use.