26 lines
828 B
Plaintext
26 lines
828 B
Plaintext
#lang rhombus/static
|
|
|
|
import lib("racket/base.rkt") expose:
|
|
#{string->list} as string_to_list
|
|
#{list->string} as list_to_string
|
|
modulo
|
|
|
|
fun make_rot13_char(key :: Int):
|
|
fun (char :: Char):
|
|
let char_int = char.to_int()
|
|
fun rot13_by_case(char_int, a_char :: Char) :: Int :
|
|
modulo(char_int - a_char.to_int() + key, 26) + a_char.to_int()
|
|
if char.is_alphabetic():
|
|
| if char.is_lowercase():
|
|
| Char.from_int(rot13_by_case(char_int, Char"a"))
|
|
| Char.from_int(rot13_by_case(char_int, Char"A"))
|
|
| char
|
|
|
|
fun rot13_string(str :: String, key :: Int) :: String :
|
|
let chars :: PairList = string_to_list(str)
|
|
let rot13_char = make_rot13_char(key)
|
|
let str :: MutableString = list_to_string(chars.map(rot13_char))
|
|
str.snapshot()
|
|
|
|
rot13_string("Hello, World!", 14) // Output: "Vszzc, Kcfzr!""
|