Skip to content

Commit d28faa2

Browse files
authored
Update MonoAlphabetic.java
1 parent 63f803f commit d28faa2

File tree

1 file changed

+43
-29
lines changed

1 file changed

+43
-29
lines changed

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

+43-29
Original file line numberDiff line numberDiff line change
@@ -2,49 +2,63 @@
22

33
public final class MonoAlphabetic {
44

5-
private final String key;
6-
7-
// Constructor to initialize the key for encryption and decryption
8-
public MonoAlphabetic(String key) {
9-
this.key = key.toUpperCase(); // Store the key in uppercase to match the encryption/decryption logic
5+
// Private constructor to prevent instantiation of utility class
6+
private MonoAlphabetic() {
7+
throw new UnsupportedOperationException("Utility class");
108
}
119

1210
// Encryption method
13-
public String encrypt(String data) {
14-
StringBuilder sb = new StringBuilder(data.toUpperCase());
15-
16-
for (int i = 0; i < sb.length(); i++) {
17-
char currentChar = sb.charAt(i);
18-
if (Character.isLetter(currentChar)) { // Check if it's a letter
19-
int index = currentChar - 'A'; // Get the index for the character
20-
sb.setCharAt(i, key.charAt(index)); // Replace with the key character
11+
public static String encrypt(String data, String key) {
12+
StringBuilder sb = new StringBuilder();
13+
14+
// Convert to uppercase to match the key mapping
15+
data = data.toUpperCase();
16+
17+
for (char c : data.toCharArray()) {
18+
if (c >= 'A' && c <= 'Z') {
19+
// Get the index (0-25) for the character
20+
int idx = c - 'A';
21+
// Append the character at the corresponding index in the key
22+
sb.append(key.charAt(idx));
23+
} else {
24+
// If character is not A-Z, append it as is
25+
sb.append(c);
2126
}
2227
}
2328
return sb.toString();
2429
}
2530

2631
// Decryption method
27-
public String decrypt(String data) {
28-
StringBuilder sb = new StringBuilder(data.toUpperCase());
29-
30-
for (int i = 0; i < sb.length(); i++) {
31-
char currentChar = sb.charAt(i);
32-
if (Character.isLetter(currentChar)) { // Check if it's a letter
33-
int index = key.indexOf(currentChar); // Find the character in the key
34-
if (index != -1) {
35-
sb.setCharAt(i, (char) (index + 'A')); // Replace with the original character
32+
public static String decrypt(String data, String key) {
33+
StringBuilder sb = new StringBuilder();
34+
35+
// Convert to uppercase to match the key mapping
36+
data = data.toUpperCase();
37+
38+
for (char c : data.toCharArray()) {
39+
if (c >= 'A' && c <= 'Z') {
40+
// Get the index from the key for the character
41+
int idx = getIndex(c, key);
42+
// Append the original character
43+
if (idx != -1) {
44+
char originalChar = (char) (idx + 'A');
45+
sb.append(originalChar);
3646
}
47+
} else {
48+
// If character is not A-Z, append it as is
49+
sb.append(c);
3750
}
3851
}
3952
return sb.toString();
4053
}
4154

42-
// Static utility methods for encryption/decryption without creating an instance
43-
public static String encrypt(String data, String key) {
44-
return new MonoAlphabetic(key).encrypt(data);
45-
}
46-
47-
public static String decrypt(String data, String key) {
48-
return new MonoAlphabetic(key).decrypt(data);
55+
// Helper method to get index of a character in the key
56+
private static int getIndex(char c, String key) {
57+
for (int i = 0; i < key.length(); i++) {
58+
if (key.charAt(i) == c) {
59+
return i; // Return the index if the character matches
60+
}
61+
}
62+
return -1; // Return -1 if character not found (should not happen for valid inputs)
4963
}
5064
}

0 commit comments

Comments
 (0)