I need to reverse wstring. I have such code:
#include <iostream>
#include <string>
#include <locale>
int main() {
std::wstring s;
std::getline(std::wcin, s);
for (const auto &i : s) {
std::wcout << (int) i << " ";
}
std::wcout << std::endl;
std::wcout << s << std::endl;
std::reverse(s.begin(), s.end());
std::wcout << s << std::endl;
return 0;
}
ANSI characters are encoded in 1 byte, and I can easily reverse them:
echo -n "papa" | ./reverse
112 97 112 97
papa
apap
But when I enter cyrillic text, that are encoded more than 1 bytes, I get such output:
echo -n "папа" | ./reverse
208 191 208 176 208 191 208 176
папа
�пап�
How can I properly reverse that string?
P.S. I'm using OS X.