Caesar Cipher Java Code

The Caesar Cipher is a famous implementation of early day encryption. It would take a sentence and reorganize it based on a key that is enacted upon the alphabet. Take, for example, a key of 3 and the sentence, “I like to wear hats.”. $ begingroup $ In France, I use to tell it 'Code Cesar', but it seems that here it is more about a 'Caesar Cipher' as 200sucess edited my post. But anyway, that's not the subject ^^. $ endgroup $ – Thomas Cloarec Nov 26 '18 at 22:57. The challenge The action of a Caesar cipher is to replace each plaintext letter (plaintext letters are from ‘a’ to ‘z’ or from ‘A’ to ‘Z’) with a different one a fixed number of places up or down the alphabet. This program performs a variation of the Caesar shift. The shift increases by 1 for Read More »First Variation on Caesar Cipher in Java.

Caesar Cipher Java CodeCodeJava

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

Challenge Name: Caesar Cipher
Problem:

Java

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.

Java

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

Sample Output

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).

Caesar Shift Cipher Java Code

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:

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 🙂

cipher.java

Caesar Cipher Java Code

importjava.io.*;
importjava.util.ArrayList;
importjava.util.Iterator;
publicclasscipher
{
publicstaticvoidmain(String[]args)
throwsjava.io.IOException
{
FileReaderReadFile=newFileReader('input.txt');
BufferedReaderReadFileBuffer=newBufferedReader(ReadFile);
FileWriterWriteFile=newFileWriter('output.txt');
BufferedWriterWriteFileBuffer=newBufferedWriter(WriteFile);
ArrayList<String> inputArray =newArrayList<String>();
String currentLine;
int lineCounter =0;
while((currentLine =ReadFileBuffer.readLine()) !=null)
{
inputArray.add(currentLine);
lineCounter++;
}
ReadFileBuffer.close();
Iterator<String> iter = inputArray.iterator();
while(iter.hasNext())
{
String line = iter.next().toUpperCase();
for(int i=0;i<line.length();i++)
WriteFileBuffer.write(cipherLetter(line.charAt(i), true));
WriteFileBuffer.write('n');
}
WriteFileBuffer.close();
}
publicstaticcharcipherLetter(charletter, booleanencode)
{
int cipherLength =3;
int ascii = (int)letter;
if (ascii>=65&& ascii<=90)
{
ascii += encode ? cipherLength :-cipherLength;
ascii = (ascii-65);
if (ascii <0 ) ascii +=25;
ascii = ascii %25+65;
}
return (char)ascii;
}
}

Caesar Cipher Java Code Generator

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment