Caesar Cipher ⬀
Julius Caesar protected his confidential information by encrypting it using a cipher. Caesar's cipher shifts each letter by a number of letters. If the shift takes you past the end of the alphabet, just rotate back to the front of the alphabet. In the case of a rotation by 3, w, x, y and z would map to z, a, b and c.
Original alphabet: abcdefghijklmnopqrstuvwxyz
Alphabet rotated +3: defghijklmnopqrstuvwxyzabc
s = There's-a-starman-waiting-in-the-sky
k = 3
The alphabet is rotated by 3, matching the mapping above. The encrypted string is
Wkuh'v-d-vwdupdq-zdlwlqj-lq-wkh-vnb.
Note: The cipher only encrypts letters; symbols, such as -, remain unencrypted.
Complete the caesarCipher function in the editor below.
caesarCipher has the following parameter(s):
string s: cleartextint k: the alphabet rotation factor
string: the encrypted string
- The first line contains the integer,
n, the length of the unencrypted string. - The second line contains the unencrypted string,
s. - The third line contains
k, the number of letters to rotate the alphabet by.
- 1 ≤
n≤ 100 - 1 ≤
k≤ 100 sis a valid ASCII string without any spaces.
11
middle-Outz
2
okffng-Qwvb
Original alphabet: abcdefghijklmnopqrstuvwxyz
Alphabet rotated +2: cdefghijklmnopqrstuvwxyzab
m -> o
i -> k
d -> f
d -> f
l -> n
e -> g
- -
O -> Q
u -> w
t -> v
z -> b