Strings, General
The Caesar Cipher is a famous enciphering algorithm said to have been favored
by Roman emperor, Julius Caesar. As legend has it, Caesar would scramble secret
messages by shifting each character by 3
. The table of plaintext characters
vs. their encoded counterparts for the Caesar Cipher looks like this:
Plaintext |
Encoded |
---|---|
A |
D |
B |
E |
C |
F |
… |
… |
X |
A |
Y |
B |
Z |
C |
The Caesar Cipher is a rotary cipher — a cipher used to encrypt or decrypt text by shifting and rotating letters in an alphabet. It’s not a very secure cipher though. It would be better if we could shift letters by any amount — not just 3!
Write a function to encode text using a rotary cipher with a variable shift. It should take in:
An integer — the number of times we should shift a letter
A string — the text to encode
The function should return the encoded text.
The given string might contain spaces, special characters, and/or numbers, but you should only shift the alphabetic characters — that is, A–Z and a–z. Be sure to preserve the capitalization of the original string.