/*
ROT13 is a simple substitution cipher that replaces a letter with the 13th letter
after it in the alphabet. Because the English alphabet has 26 letters (13 * 2),
applying ROT13 twice restores the original text (it is its own inverse).
*/
object Rot13App {
/**
* Applies the ROT13 cipher to the given string.
*
* ROT13 rotates each letter by 13 positions in the alphabet:
* A <-> N, B <-> O, ..., M <-> Z
* a <-> n, b <-> o, ..., m <-> z
*
* Non-alphabetic characters are left unchanged.
* Applying ROT13 twice returns the original string.
*
* @param input The text to encode or decode.
* @return The ROT13-transformed string.
*/
def rot13(input: String): String = {
input.map {
case c if c >= 'A' && c <= 'Z' =>
('A' + (c - 'A' + 13) % 26).toChar
case c if c >= 'a' && c <= 'z' =>
('a' + (c - 'a' + 13) % 26).toChar
case c =>
// Leave digits, spaces, punctuation, etc. unchanged.
c
}
}
def main(args: Array[String]): Unit = {
val input = "Hello, World! 123"
val encoded = rot13(input)
val decoded = rot13(encoded)
println(s"Input : $input")
println(s"Encoded : $encoded")
println(s"Decoded : $decoded")
}
}
/*
run:
Input : Hello, World! 123
Encoded : Uryyb, Jbeyq! 123
Decoded : Hello, World! 123
*/