Skip to content

Instantly share code, notes, and snippets.

@Marchuck
Created July 15, 2016 11:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Marchuck/eba3dbd282de04e386f30f152cf11025 to your computer and use it in GitHub Desktop.
Save Marchuck/eba3dbd282de04e386f30f152cf11025 to your computer and use it in GitHub Desktop.
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