#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);
}
