Caesar Cipher Hackerrank Algorithm Solution in Java

By | October 26, 2017

Hello Friends, in this tutorial we are going to learn Hackerrank Algorithm Caesar Cipher.

Challenge Name: 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 unencrypted.

Input Format

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

Constraints
1 ≤ N ≤ 100
0 ≤ K ≤ 100

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 unencrypted 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 Z 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 Video:

Solution Code:

import java.util.Scanner;
 
/**
 *
 * @author Milind
 */
public class CaesarCipher {
 
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        String s = in.next();
        int k = in.nextInt() % 26;
        String str = "";
        for (int i = 0; i < n; i++) {
            str = str + getUpdatedAsciiChar(s.charAt(i), k);
        }
        System.out.println(str);
    }
 
    static char getUpdatedAsciiChar(char ch, int incremental) {
        if (Character.isLetter(ch)) {
            int i = (int) ch;
            i = i + incremental;
 
            if (Character.isUpperCase(ch)) {
                if (i > 90) {
                    int num = (int) ch + incremental;
                    i = num - 26;
                }
            } else if (i > 122) {
                int num = (int) ch + incremental;
                i = num - 26;
            }
            return (char) i;
        }
        return ch;
    }
 
}

Challenge link:
https://www.hackerrank.com/challenges/caesar-cipher-1

Github Code link:
https://github.com/brighterapi/HackerRank-Solution/blob/master/HackerRank-Solution/HackerRankSolution/src/com/hackerranksolution/algorithms/Strings/CaesarCipher.java

Thank you 🙂