You've successfully subscribed to The Poor Coder | Hackerrank Solutions
Great! Next, complete checkout for full access to The Poor Coder | Hackerrank Solutions
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.
Hackerrank - Caesar Cipher Solution

Hackerrank - Caesar Cipher Solution

Beeze Aal
Beeze Aal

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

For example, the given cleartext  and the alphabet is rotated by . The encrypted string is .

Note: The cipher only encrypts letters; symbols, such as -, remain unencrypted.

Function Description

Complete the caesarCipher function in the editor below. It should return the encrypted string.

caesarCipher has the following parameter(s):

  • s: a string in cleartext
  • k: an integer, the alphabet rotation factor

Input Format

The first line contains the integer, , the length of the unencrypted string.
The second line contains the unencrypted string, .
The third line contains , the number of letters to rotate the alphabet by.


is a valid ASCII string without any spaces.

Output Format

For each test case, print the encoded string.

Sample Input


Sample Output



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

Solution in Python

def caesarCipher(s, k):
    e = ""
    for i in s:
        if i.islower():
            e+= chr((ord(i)-97+k)%26+97)
        elif i.isupper():
            e+= chr((ord(i)-65+k)%26+65)
            e+= i
    return e
s,k = input(), int(input())
print(caesarCipher(s, k))