Skip to content

Instantly share code, notes, and snippets.

@robbielynch
Created July 23, 2012 13:53
Show Gist options
  • Save robbielynch/3163739 to your computer and use it in GitHub Desktop.
Save robbielynch/3163739 to your computer and use it in GitHub Desktop.
Caesar Cipher - I made this caesar cipher decrypter to solve hackerrank.com puzzles.
import java.util.Scanner;
public class CaesarCipher {
private static char []letters =
{ 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'
,'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'
,'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'
};
public static void main(String args[]){
Scanner in = new Scanner(System.in);
System.out.println("Encrypted Word");
String encryptedAnswer = in.nextLine();
System.out.println("Decrypted Word");
String decryptedAnswer = in.nextLine();
System.out.println("String to decode:");
String encryptedString = in.nextLine();
String decryptedString = "";
int diff = 0;
//calculate difference between letters
encryptedAnswer = encryptedAnswer.toLowerCase();
decryptedAnswer = decryptedAnswer.toLowerCase();
char enfirst = encryptedAnswer.charAt(0);
char defirst = decryptedAnswer.charAt(0);
int e = (int) enfirst;
int d = (int) defirst;
//get difference between the 2 letters
diff = d - e;
//System.out.println("diff = " + diff);
System.out.println(encryptedString);
int startMiddleLetters = 26;
//decrypt string
for(int i = 0; i < encryptedString.length();i++){
//get current char
char curChar = encryptedString.charAt(i);
if(curChar != ' ' && curChar != ',' && curChar != '-'){
//Get number of current char
int curCharInt = (int)curChar;
//Calculate position of the current char in the middle section of the letters array
int posOfCurCharInLettersArray = (curCharInt - 97) + startMiddleLetters; //97 is 'a' in ascii
//get position of decrypted char
int posOfNewCharInLettersArray = posOfCurCharInLettersArray + diff;
//get decrypted char
char newChar = letters[posOfNewCharInLettersArray];
//build output string
decryptedString += newChar;
}else{
//build output string
decryptedString += curChar;
}
}
//output decrypted string
System.out.println(decryptedString);
}
}
@robbielynch
Copy link
Author

To become a beta user of hackerrank.com, you have to solve 100 puzzles... all of which use the Caesar Cipher.
To make things quicker, I created this...

I struggled for a while getting stringIndexOutOfBounds error because there was my ASCII calculations were off, then realised if I had a char array with 3 sets of letters from(a -> z), then by referencing the middle set of letters, it would be very easy to add and subtract the index without getting errors.

  • It made things look a bit cleaner :D

@dblandin
Copy link

great work on this!

I made a few modifications in my fork including the use of command line arguments, a StringBuffer, and modulus arithmetic to simplify the letters char array.

:: devon

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