Caesar Cipher HackerRank Solution

Caesar Cipher in an Array HackerRank Solution

In this Caesar Cipher HackerRank solution, 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

Example

The alphabet is rotated by , matching the mapping above. 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.

caesarCipher has the following parameter(s):

  • string s: cleartext
  • int k: the alphabet rotation factor

Returns

  • string: the encrypted string

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.

Constraints



 is a valid ASCII string without any spaces.

Sample Input

11
middle-Outz
2

Sample Output

okffng-Qwvb

Explanation

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

Caesar Cipher HackerRank Solution

I will Provide solution in Multiple programming languages for you. If you are not able to find the code in required language then please share in comments so that our team can help you.

Problem Solution in C++

string caesarCipher(string s, int k) {
    string myString;
    for (char c : s){
        if (isalpha(c)){
            if (isupper(c)){
                myString += (c-65+k)%26 + 65;
            }
            else {
                myString += (c-97+k)%26 + 97;
            }
        }
        else{
            myString += c;
        }
    }
    return myString;
}

Problem Solution in Python

def caesarCipher(s, k):
    a="abcdefghijklmnopqrstuvwxyz"
    k=k%26
    b=a[k:]+a[:k]
    d=''
    for i in s:
        if i.isalpha() :
            if i not in "-,'":
                if i.islower():
                    c=a.find(i)
                    d=d+b[c]
                else:
                    c=a.find(i.lower())
                    d=d+b[c].upper()  
           
        else:
            d=d+i        
    return d

Problem Solution in JavaScript

function caesarCipher(s, k) {
    // Write your code here
    let newString = '';
   
    for(let i = 0; i < s.length; i++){
       if(/[a-z]/.test(s[i])){
           let charNum = s.charCodeAt(i) + k;
           while(charNum > 122){
                  charNum -= 26;
              }
              newString += String.fromCharCode(charNum);
          }else if(/[A-Z]/.test(s[i])){
              let charNum = s.charCodeAt(i) + k;
              while(charNum > 90){
                  charNum -= 26;
                  }
                newString += String.fromCharCode(charNum);
          }else{
              newString += s[i];
          }
        }
    return newString;
}

Solve original Problem on HackerRank here. Checkout more HackerRank Problems