posted Mar 2, 2017, 11:28 PM by Samuel Konstantinovich

Goal: How NOT to hide your thoughts from the NSA.


Some examples:


OVT → BIG   






Note that ROT13 is a poor encryption method, because it is trivial to decrypt. This made it useful for posting spoilers or text that some people didn't want to see, because you must actively change the words in order to read it.

Basic algorithm:

We can go through a string, and decide what to do with each character. Append the new character to a new string, and after you append all the modified characters return that answer string.

All of these (and a few more) are here: http://codingbat.com/home/konstans@stuy.edu/all/

This is a more in-depth explanation than on the website:

rot13char(c)  which returns the rot13 value of a single character c, but only if it is a valid upper or lower case letter. It returns the original character otherwise. It MUST RETURN a value, and not print anything. 

rot13char('a')  'n'

rot13char('A')  'N'

rot13char('N')  'A'

rot13char('-')  '-'  #non letters stay the same! This is important.

2. then use that function to help write rot13(s) which returns a string that is the rot13 equivalent of the entire string s. It must return the value and not print anything. 

rot13('CLERK')  'PYREX'

rot13('sheel-HEY!')  'furry-URL!'

You will need to loop through the original string, and build a new string starting with the empty string "", then adding on the converted characters one at a time!

3. Make TWO new functions that rotate by a different number:  rotXchar(c,x) and rotX(s,x). Now you have to decide when the letters wrap. 

rotXchar('a',1) → 'b'

rotXchar('a',3) → 'd'

rotXchar('z',1)  'a'

rotXchar('z',13)  'm'    #this is like rot13

rotX('abcxyz',1) → 'bcdyza'

rotX('what',13) → 'jung'

FOR 3:

Figure this out on paper before you start writing any code: (I want to see it in your notes!)

When you rotate by 5, which letters wrap around?

When you rotate by 2, which letters wrap around?

How much does wrapping around change their values?