ROT13 ("rotate by 13 places", sometimes hyphenated ROT-13) is a simple letter substitution cipher that replaces a letter with the letter 13 letters after it in the alphabet. ROT13 is a special case of the Caesar cipher, developed in ancient Rome.
ROT47 is a derivative of ROT13 which, in addition to scrambling the basic letters, also treats numbers and common symbols. Instead of using the sequence A–Z as the alphabet, ROT47 uses a larger set of characters from the common character encoding known as ASCII. Specifically, the 7-bit printable characters, excluding space, from decimal 33 '!' through 126 '~', 94 in total, taken in the order of the numerical values of their ASCII codes, are rotated by 47 positions, without special consideration of case. For example, the character A is mapped to p, while a is mapped to 2.
Below is my implementation of ROT47 algorithm:
#include <cstring>
#include <cstdio>
#include <iostream>
std::string rot47(std::string s)
{
std::string s1 = "!\"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
std::string s2 = "PQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~!\"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNO";
std::string ret = "";
for (unsigned int i = 0; i < s.size(); i++)
{
std::size_t pos = s1.find(s[i]);
ret += s2[pos];
}
return ret;
}
int main()
{
std::string str = "HelloWorld!";
std::string retFct = rot47(str);
std::cout << retFct << std::endl;
std::string retFct2 = rot47(retFct);
std::cout << retFct2 << std::endl;
}
It works as expected but my question is : Is there something I can improve in my implementation? I don't know if there is a more direct way to get the result.