Menu Close

Julius Caesar’s Cipher Java program

Julius Caesar protected his confidential information by encrypting it in a cipher. Caesars 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
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 does not contain any spaces.

Output
For each test case, print the encoded string.

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 is “Z” is “A” ).

Selected Examples: if K = 2
“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.

Input:
String Size:  4
String:  ANNA
Rotate_Value:  1

Output:
Encrypted_String:  BOOB
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class temp{
  public static void main(String[] args)
  {
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter the size:  ");
    int numChars = Integer.parseInt(sc.nextLine());
    System.out.print("Enter the String:  ");
    char[] inputString = sc.nextLine().toCharArray();
    System.out.print("Enter Rotate Value:  ");
    int rotateValue = sc.nextInt();

    for(int i = 0;i<numChars;i++){
      char currentChar = inputString[i];

      if(Character.isLetter(currentChar))
      {
        char rotatedChar = (char)((int)currentChar+rotateValue%26);

        if(Character.isUpperCase(currentChar))
        {
          inputString[i] = ((int)rotatedChar<=90)?rotatedChar:(char)((rotatedChar-(int)'Z')+(int)'A'-1);
        }
        else
        {
          inputString[i] =((int)rotatedChar<=122)?rotatedChar:(char)((rotatedChar-(int)'z')+(int)'a'-1);
        }

      }
    }
    System.out.println("Encrypted String:  "+ new String(inputString));
  }
}

INPUT_1:
Enter the size:  8
Enter the String:  One Piece
Enter Rotate Value:  2

OUTPUT:
Encrypted String:  Qpg Rkgee


INPUT_2:
Enter the size:  11
Enter the String:  fcukthecode
Enter Rotate Value:  5

OUTPUT:
Encrypted String:  khzpymjhtij


INPUT_3:
Enter the size:  4
Enter the String:  ANNA
Enter Rotate Value:  1

OUTPUT:
Encrypted String:  BOOB


INPUT_4:
Enter the size:  3
Enter the String:  abc
Enter Rotate Value:  2

OUTPUT:
Encrypted String:  cde


INPUT_5:
Enter the size:  1
Enter the String:  z
Enter Rotate Value:  2

OUTPUT:
Encrypted String:  b


INPUT_6:
Enter the size:  1
Enter the String:  Z
Enter Rotate Value:  2

OUTPUT:
Encrypted String:  B


INPUT_7:
Enter the size:  5
Enter the String:  ABCDE
Enter Rotate Value:  2

OUTPUT:
Encrypted String:  CDEFG


INPUT_8:
Enter the size:  10
Enter the String:  Evangelion
Enter Rotate Value:  1

OUTPUT:
Encrypted String:  Fwbohfmjpo


ILLUSTRATION

Executed using javac on Linux terminal