-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.java
82 lines (55 loc) · 1.46 KB
/
Main.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
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
public class Main {
public static HashMap<Character, Integer> a1 = new HashMap<>();
static String abc = "abcdefghijklmnopqrstuvwxyz" ;
static ArrayList<Character> newchar = new ArrayList<Character>();
public static void main(String[] args){
for (int j = 0; j < abc.length(); j++) {
a1.put(abc.charAt(j), j);
}
System.out.println(coder("zygomatique", 28));
System.out.println(decoder("baiqocvkswg", 2));
}
public static String coder(String s, int i){
newchar.clear();
for(char c : s.toCharArray()){
int pos = a1.get(c);
int newpos;
if(pos + i < 26){
newpos = pos + (i);
}else{
newpos = (pos + i)%26;
}
char nc = (char) a1.keySet().toArray()[newpos];
newchar.add(nc);
}
return chartoString(newchar);
}
public static String decoder(String s, int i){
newchar.clear();
for(char c : s.toCharArray()){
int pos = a1.get(c);
int newpos;
if(pos - i >= 0){
newpos = pos - (i);
}else if((i - pos) <= 26){
newpos = 26 - (i - pos);
}else{
newpos = 26%(26 - (i - pos));
}
char nc = (char) a1.keySet().toArray()[newpos];
newchar.add(nc);
}
return chartoString(newchar);
}
public static String chartoString(ArrayList<Character> c){
Iterator<Character> it = c.iterator();
StringBuilder sb = new StringBuilder();
while(it.hasNext()) {
sb.append(it.next());
}
return sb.toString();
}
}