Before TikTok, Instagram and Snapchat, social media consisted primarily of text-based exchanges in USENET groups (imagine Reddit without the pictures). There were many tools to facilitate these conversations, including ROT13. Using ROT13 was the customary way to avoid spoiling a joke or giving away the plot of a movie. For example, you could write a joke like this:
Why did the chicken cross the road?
Gb trg gb gur bgure fvqr!
and everyone knew to apply ROT13 to decode the punch line.
ROT13 is a simple Caesar cypher that ROT-ates each letter forward 13 places, looping around to the beginning once you reach Z.
ROT13 is applied only to the Latin letters A through Z (and again to the lowercase letters a through z). Numbers, punctuation and spaces are not shifted.
Write a program that will read the contents of the file "input.txt", apply the ROT13 transformation to its contents, and save the results to "output.txt". You can make your own "input.txt" file and check your program by running it a second time. (You have to rename your "output.txt" to "input.txt"). If you did everything correctly, the transformation will make a third file that is identical to the first.
Alternatively, you can use the files "input1.txt" and "input2.txt". You should get an output recognisable as normal English text. (Remember to rename the files to match the filename specified in your program.)
Here are my solutions in C++ and in Python.
In C++, a string is a type of vector. This means you can access the
individual characters of myString
using brackets like
this: myString[3]
(this gives you the fourth caracter of
the string).
To build a string character by character, you can call
myString.push_back(myChar)
. This will add
myChar
to the end of myString
. If you are
concerned about the overhead of reallocation, call
myString.reserve(MAX_LEN)
before starting to build the
string. Do not call reserve()
too often, such as in a loop with push_back()
.
This challenge required you to open, read, write, and close files. In C++ you should have used the standard string and vector libraries, which provide features you will need in many programs.
A natural way to implement ROT13 in most languages is with the modulo operator. This operator has many applictions in programming, such as to construct a circular queue, implement a hash table, create a random number within a specified range, etc.