-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquestion2.java
139 lines (139 loc) · 4.37 KB
/
question2.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import java.util.*;
public class question2 {
public static void main(String[] args) {
question2 qn = new question2();
int n;
while(true){
n = qn.menu();
if(n==3){
System.out.println("Goodbye!");
break;
}
System.out.println("Enter Text to Decrypt:");
String text = qn.getInputForQ(n);
System.out.println(n==1?qn.tech1(text):qn.tech2(text));
}
}
private String getInputForQ(int n) {
String text = acceptStr().toUpperCase().trim();
if(n==1){
while (true) {
boolean chk = true;
for(int i=0;i<text.length();i++){
if((text.charAt(i)<65||text.charAt(i)>90)&&text.charAt(i)!=32){
chk=false;
text = acceptStr().toUpperCase().trim();
break;
}
}
if(chk){
break;
}
}
}else if(n==2){
while (true) {
boolean chk=true;
for(int i=0;i<text.length();i++){
if((text.charAt(i)<48||text.charAt(i)>57)&&text.charAt(i)!=32){
chk=false;
text = acceptStr().toUpperCase().trim();
break;
}
}
if(chk){
break;
}
}
}
return text;
}
public String tech1(String txt){
int n=0;
while(!(n>=1&&n<=26)){
System.out.println("Enter Shift:");
n = acceptNo();
}
HashMap<String, String> decryptMap=decryptTech1Code(n);
String[] txts=txt.split(" ");
String s = "";
for (int i = 0; i < txts.length; i++) {
for(int j=0;j<txts[i].length();j++){
s+=decryptMap.get(Character.toString(txts[i].charAt(j)));
}
}
s=s.replaceAll("QQ", " ");
return s;
}
private HashMap<String, String> decryptTech1Code(int n) {
n=n-1;
String alpha="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String shift=alpha.substring(alpha.length()-n)+alpha.substring(0,alpha.length()-n);
HashMap<String, String> tmp=new HashMap<String, String>();
for (int i = 0; i < 26; i++) {
tmp.put(Character.toString(shift.charAt(i)), Character.toString(alpha.charAt(i)));
}
return tmp;
}
public String tech2(String txt){
String flip = "";
for(int i=0;i<txt.length();i++){
if(txt.charAt(i)==' '){
continue;
}
flip=Character.toString(txt.charAt(i))+flip;
}
String text="";
for(int i=0;i<flip.length();i++){
if(Integer.parseInt(flip.substring(i,i+2))<65&&Integer.parseInt(flip.substring(i,i+2))!=32){
if(i+3>flip.length()){
text += (char)(Integer.parseInt(flip.substring(i)));
}else{
text += (char)(Integer.parseInt(flip.substring(i,i+3)));
}
i+=2;
}else{
text += (char)(Integer.parseInt(flip.substring(i,i+2)));
i++;
}
}
return text;
}
public int menu(){
System.out.println("Choose your option:");
System.out.println("1.Decyption Technique 1 (Shift Mthd)");
System.out.println("2.Decyption Technique 2 (ASCII Mthd)");
System.out.println("3.Exit");
System.out.println("Enter choice number to proceed");
int n=acceptNo();
if(n<1||n>3){
System.out.println("Invalid Option!");
return menu();
}else{
return n;
}
}
public int acceptNo(){
Scanner in = new Scanner(System.in);
int n=0;
boolean flag = false;
try {
n = in.nextInt();
} catch (Exception e) {
flag=true;
n=-1;
}
return flag?acceptNo():n;
}
public String acceptStr(){
Scanner in = new Scanner(System.in);
String n;
boolean flag = false;
try {
n = in.nextLine();
} catch (Exception e) {
flag=true;
n="";
}
return flag?acceptStr():n;
}
}