RosettaCodeData/Task/Caesar-cipher/Groovy/caesar-cipher-2.groovy

8 lines
279 B
Groovy

def caesarEncode(cipherKey, text) {
text.chars.collect { c ->
int off = c.isUpperCase() ? 'A' : 'a'
c.isLetter() ? (((c as int) - off + cipherKey) % 26 + off) as char : c
}.join()
}
def caesarDecode(cipherKey, text) { caesarEncode(26 - cipherKey, text) }