-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathreverserot.java
89 lines (58 loc) · 1.38 KB
/
reverserot.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class reverserot {
public static String rotate(String str, int rot)
{
final String abc = "ABCDEFGHIJKLMNOPQRSTUVWXYZ_.";
if(abc.indexOf(str) + rot < abc.length())
{
return abc.substring(abc.indexOf(str) + rot, abc.indexOf(str) + rot+1);
}
else
{
return abc.substring( ((abc.indexOf(str) + rot) % 28), (abc.indexOf(str) + rot) % 28+1);
}
}
public static String reverse(String str)
{
char[] word = str.toCharArray();
ArrayList<String> almost = new ArrayList<>();
for(char c: word)
{
almost.add(c + "");
}
Collections.reverse(almost);
String ans = "";
for(String a : almost)
{
ans += a;
}
return ans;
}
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
//System.out.println(rotate(".",1));
while(true)
{
int rots = scan.nextInt();
if(rots == 0)
{
break;
}
else
{
String str = scan.nextLine();
str = reverse(str);
String ans = "";
for(int i=0; i < str.length(); i++)
{
ans += rotate(str.substring(i, i+1), rots);
}
System.out.println(ans.substring(0,ans.length()-1));
}
}
scan.close();
}
}