|
1 | 1 | package com.thealgorithms.ciphers; |
2 | 2 |
|
3 | 3 | /** |
4 | | - * The Atbash cipher is a simple substitution cipher that replaces each letter |
5 | | - * in the alphabet with its reverse. |
6 | | - * For example, 'A' becomes 'Z', 'B' becomes 'Y', and so on. It works |
7 | | - * identically for both uppercase and lowercase letters. |
8 | | - * It's a symmetric cipher, meaning applying it twice returns the original text. |
9 | | - * Hence, the encrypting and the decrypting functions are identical |
10 | | - * @author https://github.com/Krounosity |
11 | | - * Learn more: https://en.wikipedia.org/wiki/Atbash |
| 4 | + * The Atbash cipher is a classic substitution cipher that substitutes each letter |
| 5 | + * with its opposite letter in the alphabet. |
| 6 | + * |
| 7 | + * For example: |
| 8 | + * - 'A' becomes 'Z', 'B' becomes 'Y', 'C' becomes 'X', and so on. |
| 9 | + * - Similarly, 'a' becomes 'z', 'b' becomes 'y', and so on. |
| 10 | + * |
| 11 | + * The cipher works identically for both uppercase and lowercase letters. |
| 12 | + * Non-alphabetical characters remain unchanged in the output. |
| 13 | + * |
| 14 | + * This cipher is symmetric, meaning that applying the cipher twice will return |
| 15 | + * the original text. Therefore, the same function is used for both encryption and decryption. |
| 16 | + * |
| 17 | + * <p>Usage Example:</p> |
| 18 | + * <pre> |
| 19 | + * AtbashCipher cipher = new AtbashCipher("Hello World!"); |
| 20 | + * String encrypted = cipher.convert(); // Output: "Svool Dliow!" |
| 21 | + * </pre> |
| 22 | + * |
| 23 | + * @author <a href="https://github.com/Krounosity">Krounosity</a> |
| 24 | + * @see <a href="https://en.wikipedia.org/wiki/Atbash">Atbash Cipher (Wikipedia)</a> |
12 | 25 | */ |
13 | | - |
14 | 26 | public class AtbashCipher { |
15 | 27 |
|
16 | 28 | private String toConvert; |
17 | 29 |
|
18 | | - // Default constructor. |
19 | | - AtbashCipher() { |
| 30 | + public AtbashCipher() { |
20 | 31 | } |
21 | 32 |
|
22 | | - // String setting constructor. |
23 | | - AtbashCipher(String str) { |
24 | | - toConvert = str; |
| 33 | + /** |
| 34 | + * Constructor with a string parameter. |
| 35 | + * |
| 36 | + * @param str The string to be converted using the Atbash cipher |
| 37 | + */ |
| 38 | + public AtbashCipher(String str) { |
| 39 | + this.toConvert = str; |
25 | 40 | } |
26 | 41 |
|
27 | | - // String getter method. |
| 42 | + /** |
| 43 | + * Returns the current string set for conversion. |
| 44 | + * |
| 45 | + * @return The string to be converted |
| 46 | + */ |
28 | 47 | public String getString() { |
29 | 48 | return toConvert; |
30 | 49 | } |
31 | 50 |
|
32 | | - // String setter method. |
| 51 | + /** |
| 52 | + * Sets the string to be converted using the Atbash cipher. |
| 53 | + * |
| 54 | + * @param str The new string to convert |
| 55 | + */ |
33 | 56 | public void setString(String str) { |
34 | | - toConvert = str; |
| 57 | + this.toConvert = str; |
35 | 58 | } |
36 | 59 |
|
37 | | - // Checking whether the current character is capital. |
| 60 | + /** |
| 61 | + * Checks if a character is uppercase. |
| 62 | + * |
| 63 | + * @param ch The character to check |
| 64 | + * @return {@code true} if the character is uppercase, {@code false} otherwise |
| 65 | + */ |
38 | 66 | private boolean isCapital(char ch) { |
39 | 67 | return ch >= 'A' && ch <= 'Z'; |
40 | 68 | } |
41 | 69 |
|
42 | | - // Checking whether the current character is smallcased. |
| 70 | + /** |
| 71 | + * Checks if a character is lowercase. |
| 72 | + * |
| 73 | + * @param ch The character to check |
| 74 | + * @return {@code true} if the character is lowercase, {@code false} otherwise |
| 75 | + */ |
43 | 76 | private boolean isSmall(char ch) { |
44 | 77 | return ch >= 'a' && ch <= 'z'; |
45 | 78 | } |
46 | 79 |
|
47 | | - // Converting text to atbash cipher code or vice versa. |
| 80 | + /** |
| 81 | + * Converts the input string using the Atbash cipher. |
| 82 | + * Alphabetic characters are substituted with their opposite in the alphabet, |
| 83 | + * while non-alphabetic characters remain unchanged. |
| 84 | + * |
| 85 | + * @return The converted string after applying the Atbash cipher |
| 86 | + */ |
48 | 87 | public String convert() { |
49 | | - |
50 | | - // Using StringBuilder to store new string. |
51 | 88 | StringBuilder convertedString = new StringBuilder(); |
52 | 89 |
|
53 | | - // Iterating for each character. |
54 | 90 | for (char ch : toConvert.toCharArray()) { |
55 | | - |
56 | | - // If the character is smallcased. |
57 | 91 | if (isSmall(ch)) { |
58 | 92 | convertedString.append((char) ('z' - (ch - 'a'))); |
59 | | - } |
60 | | - // If the character is capital cased. |
61 | | - else if (isCapital(ch)) { |
| 93 | + } else if (isCapital(ch)) { |
62 | 94 | convertedString.append((char) ('Z' - (ch - 'A'))); |
63 | | - } |
64 | | - // Non-alphabetical character. |
65 | | - else { |
| 95 | + } else { |
66 | 96 | convertedString.append(ch); |
67 | 97 | } |
68 | 98 | } |
|
0 commit comments