Skip to content

Instantly share code, notes, and snippets.

@Marchuck
Created Jul 15, 2016
Embed
What would you like to do?
public static void main(String[] argv) {
new CesarCipher().solve();
}
public void solve() {
Scanner in = new Scanner(System.in);
int t = Integer.valueOf(in.nextLine());
String line = in.nextLine();
StringBuilder sb = new StringBuilder();
String keys = in.nextLine();
int key = Integer.valueOf(keys);
for (int x = 0; x < t; x++) {
int code = line.charAt(x);
int characterType = getCharacterType(code);
char ch;
if (characterType == 1) {
if (code + key > 90) ch = (char) (code + key - 24);
else ch = (char) (code + key);
} else {
if (code + key > 122) ch = (char) (code + key - 24);
else ch = (char) (code + key);
}
sb.append(ch);
}
System.out.println(sb.toString());
}
private static int getCharacterType(int code) {
if (code >= 65 && code <= 90) return 1;
else if (code >= 97 && code <= 122) return 2;
else return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment