File tree 1 file changed +31
-0
lines changed
1 file changed +31
-0
lines changed Original file line number Diff line number Diff line change
1
+ // https://www.hackerrank.com/challenges/caesar-cipher-1/problem
2
+
3
+ package strings ;
4
+
5
+ import java .util .Scanner ;
6
+
7
+ public class CaesarCipher {
8
+ public static void main (String [] args ) {
9
+ Scanner scanner = new Scanner (System .in );
10
+ int length = scanner .nextInt ();
11
+ String string = scanner .next ();
12
+ int rotations = scanner .nextInt ();
13
+ System .out .println (encrypted (string , rotations ));
14
+ }
15
+
16
+ private static String encrypted (String string , int rotations ) {
17
+ StringBuilder accumulator = new StringBuilder ();
18
+ for (int index = 0 ; index < string .length () ; index ++) {
19
+ char character = string .charAt (index );
20
+ accumulator .append (Character .isAlphabetic (character ) ? encrypted (character , rotations ) : character );
21
+ }
22
+ return accumulator .toString ();
23
+ }
24
+
25
+ private static char encrypted (char character , int rotations ) {
26
+ if (Character .isLowerCase (character )) {
27
+ return (char ) ((character - 97 + rotations ) % 26 + 97 );
28
+ }
29
+ return (char ) ((character - 65 + rotations ) % 26 + 65 );
30
+ }
31
+ }
You can’t perform that action at this time.
0 commit comments