Skip to content

Commit 3e9faea

Browse files
authored
Update MonoAlphabetic.java
1 parent bbc6927 commit 3e9faea

File tree

1 file changed

+29
-32
lines changed

1 file changed

+29
-32
lines changed

src/main/java/com/thealgorithms/ciphers/MonoAlphabetic.java

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,68 +8,65 @@ public static void main(String[] args) {
88
String name = read.nextLine();
99
read.nextLine();
1010

11-
System.out.println("Welcome "+name+"!\nDo you want to encrypt data or decrypt the data?\nFor encryption enter: 1\nFor decryption enter: 2");
12-
int x = read.nextInt();
11+
System.out.println("Welcome " + name + "!\nDo you want to encrypt data or decrypt the data?\nFor encryption enter: 1\nFor decryption enter: 2"); int x = read.nextInt();
1312
read.nextLine();
1413

1514
String key = "MNBVCXZLKJHGFDSAPOIUYTREWQ";
1615

1716
switch (x) {
1817
case 1:
19-
System.out.println("\nPlease enter the data that is to be encrypted, we will be using MonoAlphabetic Cipher to encrypt the data.");
20-
String data = read.nextLine().toUpperCase();;
21-
22-
String encryptedData = encrypt(data,key);
23-
System.out.println("Encrypted data: " + encryptedData);
24-
break;
25-
26-
case 2:
27-
System.out.println("\nPlease enter the data that is to be decrypted, we will be using MonoAlphabetic Cipher to decrypt the data.");
28-
data = read.nextLine().toUpperCase();;
29-
30-
String decryptedData = decrypt(data,key);
31-
System.out.println("Decrypted data: " + decryptedData);
32-
break;
33-
34-
default:
35-
System.out.println("The input was invalid. Kindly restart.");
36-
break;
37-
}
38-
39-
18+
System.out.println("\nPlease enter the data that is to be encrypted, we will be using MonoAlphabetic Cipher to encrypt the data.");
19+
String data = read.nextLine().toUpperCase();
20+
21+
String encryptedData = encrypt(data, key);
22+
System.out.println("Encrypted data: " + encryptedData);
23+
break;
24+
25+
case 2:
26+
System.out.println("\nPlease enter the data that is to be decrypted, we will be using MonoAlphabetic Cipher to decrypt the data.");
27+
data = read.nextLine().toUpperCase();
28+
29+
String decryptedData = decrypt(data, key);
30+
System.out.println("Decrypted data: " + decryptedData);
31+
break;
32+
33+
default:
34+
System.out.println("The input was invalid. Kindly restart.");
35+
break;
36+
}
4037
}
4138

42-
public static String encrypt(String data, String key){
39+
public static String encrypt(String data, String key) {
4340
int idx;
4441
char c;
4542
StringBuffer sb = new StringBuffer(data);
4643

47-
for(int i=0; i<sb.length(); i++){
48-
idx = sb.charAt(i)-65;
44+
for(int i=0; i<sb.length(); i++) {
45+
idx = sb.charAt(i) - 65;
4946
c = key.charAt(idx);
5047
sb.setCharAt(i, c);
5148
}
5249
return new String(sb);
5350
}
5451

55-
public static String decrypt(String data, String key){
52+
public static String decrypt(String data, String key) {
5653
int idx;
5754
char c;
5855
StringBuffer sb = new StringBuffer(data);
5956

60-
for(int i=0; i<sb.length(); i++){
57+
for(int i=0; i<sb.length(); i++) {
6158
c = sb.charAt(i);
62-
idx = getIndex(c,key);
59+
idx = getIndex(c, key);
6360
c = (char) (idx + 65);
6461
sb.setCharAt(i, c);
6562
}
6663
return new String(sb);
6764
}
6865

69-
public static int getIndex(char c, String key){
66+
public static int getIndex(char c, String key) {
7067
int idx = -1;
71-
for(int i=0; i<key.length();i++){
72-
if(key.charAt(i) == c){
68+
for(int i=0; i<key.length();i++) {
69+
if(key.charAt(i) == c) {
7370
idx = i;
7471
}
7572
}

0 commit comments

Comments
 (0)