-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaesar_cipher.java
More file actions
50 lines (37 loc) · 978 Bytes
/
caesar_cipher.java
File metadata and controls
50 lines (37 loc) · 978 Bytes
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
//solution to: https://www.hackerrank.com/challenges/caesar-cipher-1
public class caesar_cipher {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
String str = scan.next();
int k = scan.nextInt();
int ascii = 0;
String ascii_rotate = "";
StringBuilder output = new StringBuilder();
for (int i = 0; i < n; i++) {
int tmp = (int) str.charAt(i);
if ((tmp <= 122) && (tmp >= 97)) {
ascii = tmp + k;
while (ascii > 122) {
ascii = ascii - 26;
}
} else if ((tmp <= 90) && (tmp >= 65)) {
ascii = tmp + k;
while (ascii > 90) {
ascii = ascii - 26;
}
} else {
ascii = tmp;
}
ascii_rotate = Character.toString((char) ascii);
output.append(ascii_rotate);
}
String finalstring = output.toString();
System.out.println(finalstring);
}
}