public class CaesarCipher {
public static void main(String[] args) {
String[] 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"};
String message1 = "Kfzb gly!";
System.out.println(cipher(message1));
String message2 = "zlab zlab zlab";
System.out.println(cipher(message2));
String message3 = "prmbozxifcoxdfifpqfzbumfxifalzflrp";
System.out.println(cipher(message3));
}
public static String cipher (String message){
String decoded = "";
//for every letter in the message
for (int i = 0; i < message.length(); i++){
//charAt returns value at specified index
//if the character at the index is greater than the index of a but less than the index of z
if(message.charAt(i) < 'a' || message.charAt(i) > 'z'){
decoded += message.charAt(i);
continue;
}
//the index of the letter
int letter = message.charAt(i)-'a';
//adds three and uses the modulo operator to ensure it doesn't overflow
int new_letter = (letter+3)%26;
decoded += (char)(new_letter+'a');
}
return decoded;
}
}
CaesarCipher.main(null);