The rot13 “encryption” program is one of the first programs that new developers learn to write. Requiring only one variable, the majority of the program is an exercise in how to use the if statement to compare values.
I first encountered rot13 when I was an undergraduate student in the early 1990s. If you posted online and gave spoilers to a TV show or movie, it was polite to hide the spoiler using rot13. If the other person didn’t mind seeing the spoiler, they used rot13 to view your answer.
The rot13 program is very simple to write in the C programming language, so let’s write our own version here:
How it works
The rules for rot13 are fairly straightforward: Rotate any letters in the input by 13 positions in the alphabet. More practically, if it’s a letter between A and M, replace it with a letter from N to Z. Or if it’s a letter between N and Z, replace it with a letter from A to M. The same method applies to lowercase letters, too.
A neat feature of rot13 is that you can apply the method a second time to un-hide the message, because the English language uses 26 letters. Using rot13 twice always gives you the original message.
The core of the rot13 program evaluates a message one letter at a time. For each letter, if it’s between A–M or a–m, the program prints a new letter that’s 13 positions further down the alphabet. Using ASCII, you can just add 13 to the original letter:
if (((c >= 'A') && (c <= 'M'))
|| ((c >= 'a') && (c <= 'm'))) {
putchar(c + 13);
}
If the letter is between N–Z or n–z, then the program replaces it with a letter that’s 13 positions “up” in the alphabet:
else if (((c >= 'N') && (c <= 'Z'))
|| ((c >= 'n') && (c <= 'z'))) {
putchar(c - 13);
}
For any other character, such as punctuation, a number, or a special symbol, the program simply prints that character as-is:
else {
putchar(c);
}
All that is left is adding some code to read data one letter at a time. For this, we could use a neat feature of the C programming language to read a character with getchar, store it, and evaluate it at the same time (using while ((c = getchar()) != EOF) { }) but I think the program will be easier for new programmers to understand if I use a do loop instead:
#include <stdio.h>
int main()
{
int c;
do {
c = getchar();
if (c != EOF) {
if (((c >= 'A') && (c <= 'M'))
|| ((c >= 'a') && (c <= 'm'))) {
putchar(c + 13);
}
else if (((c >= 'N') && (c <= 'Z'))
|| ((c >= 'n') && (c <= 'z'))) {
putchar(c - 13);
}
else {
putchar(c);
}
}
} while (c != EOF);
return 0;
}
If you save this program as rot13.c and compile it using a C compiler (such as GCC on Linux) you will get a program called rot13 that reads characters one at a time and “rotates” letters by 13 positions.
$ gcc -o rot13 rot13.c
Testing the program
Let’s give the program a simple test case to see that it works. We know that any letter from A–M will be translated to N–Z. That means if we give it the input Abc we should expect the output Nop. But any other non-letter character will remain untouched, such as 123:
$ echo Abc123 | ./rot13
Nop123
We can also send a simple message through the rot13 program to see that the new message is hidden:
$ echo Hello, World! | ./rot13
Uryyb, Jbeyq!
And if we use rot13 twice, we will see the original message:
$ echo Hello, World! | ./rot13 | ./rot13
Hello, World!
Finally, we can use the program to hide the spoiler to a TV show or movie, which we can later copy and paste into an email message or blog post:
$ echo the butler did it. | ./rot13
gur ohgyre qvq vg.
And the message that I post online might look like this:
I was very disappointed in the latest Sherlock Holmes movie, because it was obvious: gur ohgyre qvq vg.