HackerRank : 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
To pass an encrypted message from one person to another, it is first necessary that both parties have the 'key' for the cipher, so that the sender may encrypt it and the receiver may decrypt it.
The key is the number of OFFSETs to shift the cipher alphabet.
As we are designing custom Caesar Cipher, in addition to alphabets, we are considering numeric digits from 0 to 9. Digits can also be shifted by key places (in circular fashion).
For example:
If given plain text contains any digit with value 5 and key = 2, then 5 will be replaced by 7.
If given plain text contains any digit with value 9 and key = 1, then 9 will be replaced by 0.
Note: The cipher only encrypts letters; symbols, such as -, remain unencrypted.
Key value less than 0 should result into "INVALID INPUT"
Input Format
Write a program which will accept plain text and key as input parameters and returns its cipher text as output.
Constraints
None
Output Format
Encrypted text
Sample Input 0
All the Best
1
Sample Output 0
The encrypted text is: Bmm uif Cftu
Sample Input 1
ATTACK AT ONCE
4
Sample Output 1
The encrypted text is: EXXEGO EX SRGI
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
}
}
Solution and explanation welcome ☻