Created
July 15, 2016 11:54
-
-
Save Marchuck/eba3dbd282de04e386f30f152cf11025 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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