Rot13 in C++

Published on 19 October 2023 (Updated: 05 May 2026)

Welcome to the Rot13 in C++ page! Here, you'll find the source code for this program as well as a description of how the program works.

Current Solution

#include <algorithm>
#include <cctype>
#include <iostream>
#include <string_view>

[[noreturn]] void usage() {
    std::cerr << "Usage: please provide a string to encrypt\n";
    std::exit(1);
}

constexpr char rot13_char(char c) {
    if (c >= 'a' && c <= 'z') {
        return static_cast<char>('a' + (c - 'a' + 13) % 26);
    }
    if (c >= 'A' && c <= 'Z') {
        return static_cast<char>('A' + (c - 'A' + 13) % 26);
    }
    return c;
}

int main(int argc, char* argv[]) {
    if (argc != 2 || std::string_view(argv[1]).empty()) usage();

    std::string s = argv[1];
    std::ranges::transform(s, s.begin(), rot13_char);
    std::cout << s << '\n';
}

Rot13 in C++ was written by:

If you see anything you'd like to change or update, please consider contributing.

How to Implement the Solution

No 'How to Implement the Solution' section available. Please consider contributing.

How to Run the Solution

No 'How to Run the Solution' section available. Please consider contributing.