HackerRank: Caesar Cipher

Problem

Julius Caesar protected his confidential information by encrypting it in a cipher. Caesar’s cipher rotated every letter in a string by a fixed number, K, making it unreadable by his enemies. Given a string, S, and a number, K, encrypt S and print the resulting string.

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

Input Format

The first line contains an integer, N, which is the length of the non-encrypted string.
The second line contains the non-encrypted string, S.
The third line contains the integer encryption key, K, which is the number of letters to rotate.

Constraints
1N100
0K100
S is a valid ASCII string and doesn’t contain any spaces.

Output Format

For each test case, print the encoded string.

Sample Input

11
middle-Outz
2

Sample Output

okffng-Qwvb

Explanation

Each non-encrypted letter is replaced with the letter occurring K spaces after it when listed alphabetically. Think of the alphabet as being both case-sensitive and circular; if K rotates past the end of the alphabet, it loops back to the beginning (i.e.: the letter after z is a, and the letter after is A).

Selected Examples:
m (ASCII 109) becomes o (ASCII 111).
i (ASCII 105) becomes k (ASCII 107).
remains the same, as symbols are not encoded.
O (ASCII 79) becomes Q (ASCII 81).
z (ASCII 122) becomes b (ASCII 98); because z is the last letter of the alphabet,
a (ASCII 97) is the next letter after it in lower-case rotation.

Solution

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.