Caesar's Cipher
Julius Caesar had many secrets. He used a simple form of encryption to hide his secrets from his enemies. The cipher was easy: shift the alphabet over by a number of letters and replace each letter with the shifted version.
For instance, if you shift the alphabet by two, you get:
a b c d e f g h i j k l m n o p q r s t u v w x y z
c d e f g h i j k l m n o p q r s t u v w x y z a b
When encrypting a string, we replace instances of the top letter with the lower letter. m
becomes o
. And notice that the alphabet wraps around.
This was cutting edge technology back in those times! You can get this as a prize in a Cracker Jack box :)
Anyway, write a function that will encrypt a string. It should take the shift number and the string. Leave non-letters alone and keep caps in place. You only have to deal with regular ASCII letters for simplicity.
Bonus: make it so to decrypt it you can pass in a negative number.
Thanks to this site for the idea.