-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCaesarCipher.java
31 lines (26 loc) · 1.07 KB
/
CaesarCipher.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// https://www.hackerrank.com/challenges/caesar-cipher-1/problem
package strings;
import java.util.Scanner;
public class CaesarCipher {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int length = scanner.nextInt();
String string = scanner.next();
int rotations = scanner.nextInt();
System.out.println(encrypted(string, rotations));
}
private static String encrypted(String string, int rotations) {
StringBuilder accumulator = new StringBuilder();
for (int index = 0 ; index < string.length() ; index++) {
char character = string.charAt(index);
accumulator.append(Character.isAlphabetic(character) ? encrypted(character, rotations) : character);
}
return accumulator.toString();
}
private static char encrypted(char character, int rotations) {
if (Character.isLowerCase(character)) {
return (char) ((character - 97 + rotations) % 26 + 97);
}
return (char) ((character - 65 + rotations) % 26 + 65);
}
}