MCPcopy Create free account
hub / github.com/TheAlgorithms/Java / AtbashCipher

Class AtbashCipher

src/main/java/com/thealgorithms/ciphers/AtbashCipher.java:26–101  ·  view source on GitHub ↗

The Atbash cipher is a classic substitution cipher that substitutes each letter with its opposite letter in the alphabet. For example: - 'A' becomes 'Z', 'B' becomes 'Y', 'C' becomes 'X', and so on. - Similarly, 'a' becomes 'z', 'b' becomes 'y', and so on. The cipher works identically for both upp

Source from the content-addressed store, hash-verified

24 * @see <a href="https://en.wikipedia.org/wiki/Atbash">Atbash Cipher (Wikipedia)</a>
25 */
26public class AtbashCipher {
27
28 private String toConvert;
29
30 public AtbashCipher() {
31 }
32
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;
40 }
41
42 /**
43 * Returns the current string set for conversion.
44 *
45 * @return The string to be converted
46 */
47 public String getString() {
48 return toConvert;
49 }
50
51 /**
52 * Sets the string to be converted using the Atbash cipher.
53 *
54 * @param str The new string to convert
55 */
56 public void setString(String str) {
57 this.toConvert = str;
58 }
59
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 */
66 private boolean isCapital(char ch) {
67 return ch >= 'A' && ch <= 'Z';
68 }
69
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 */
76 private boolean isSmall(char ch) {
77 return ch >= 'a' && ch <= 'z';
78 }
79
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.

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected