#include <iostream>
#include <string>
#include <cctype>
#include <algorithm>
std::string caesar(std::string input, int shift)
{
static const std::string alphabet = "abcdefghijklmnopqrstuvwxyz";
static std::string cypher = alphabet;
auto middle = (shift > 0 ? alphabet.begin() : alphabet.end()) + shift;
std::rotate_copy(alphabet.begin(), middle, alphabet.end(), cypher.begin());
std::transform(input.begin(), input.end(), input.begin(), [&cypher](char c)
{
std::string::size_type pos = alphabet.find(tolower(c));
return (pos != std::string::npos ? cypher[pos] : c);
});
return input;
}
int main()
{
std::string x = "Hello, world!";
std::string y = caesar(x, 5);
std::cout << y << '\n' << caesar(y, -5);
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8c3RyaW5nPgojaW5jbHVkZSA8Y2N0eXBlPgojaW5jbHVkZSA8YWxnb3JpdGhtPgoKCnN0ZDo6c3RyaW5nIGNhZXNhcihzdGQ6OnN0cmluZyBpbnB1dCwgaW50IHNoaWZ0KQp7CiAgICBzdGF0aWMgY29uc3Qgc3RkOjpzdHJpbmcgYWxwaGFiZXQgPSAiYWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXoiOwogICAgc3RhdGljICAgICAgIHN0ZDo6c3RyaW5nIGN5cGhlciAgID0gYWxwaGFiZXQ7CiAgICBhdXRvIG1pZGRsZSA9IChzaGlmdCA+IDAgPyBhbHBoYWJldC5iZWdpbigpIDogYWxwaGFiZXQuZW5kKCkpICsgc2hpZnQ7CiAgICBzdGQ6OnJvdGF0ZV9jb3B5KGFscGhhYmV0LmJlZ2luKCksIG1pZGRsZSwgYWxwaGFiZXQuZW5kKCksIGN5cGhlci5iZWdpbigpKTsKICAgIHN0ZDo6dHJhbnNmb3JtKGlucHV0LmJlZ2luKCksIGlucHV0LmVuZCgpLCBpbnB1dC5iZWdpbigpLCBbJmN5cGhlcl0oY2hhciBjKQogICAgICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICAgICAgc3RkOjpzdHJpbmc6OnNpemVfdHlwZSBwb3Mg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