RosettaCodeData/Task/Caesar-cipher/Julia/caesar-cipher.julia

16 lines
443 B
Text
Raw Permalink Normal View History

2018-06-22 20:57:24 +00:00
function rot(ch::Char, key::Integer)
if key < 1 || key > 25 end
if isalpha(ch)
shft = ifelse(islower(ch), 'a', 'A')
ch = (ch - shft + key) % 26 + shft
2014-01-17 05:32:22 +00:00
end
2018-06-22 20:57:24 +00:00
return ch
2014-01-17 05:32:22 +00:00
end
2018-06-22 20:57:24 +00:00
rot(str::AbstractString, key::Integer) = map(x -> rot(x, key), str)
2014-01-17 05:32:22 +00:00
2018-06-22 20:57:24 +00:00
msg = "The five boxing wizards jump quickly"
2014-01-17 05:32:22 +00:00
key = 3
2018-06-22 20:57:24 +00:00
invkey = 26 - 3
2014-01-17 05:32:22 +00:00
2018-06-22 20:57:24 +00:00
println("# original: $msg\n encrypted: $(rot(msg, key))\n decrypted: $(rot(rot(msg, key), invkey))")