RosettaCodeData/Task/Caesar-cipher/Picat/caesar-cipher.picat

24 lines
634 B
Plaintext

main =>
S = "All human beings are born free and equal in dignity and rights.",
println(S),
println(caesar(S,5)),
println(caesar(caesar(S,5),-5)),
nl.
caesar(String, N) = Cipher =>
Lower = [chr(I): I in 97..122],
Upper = [chr(I): I in 65..90],
M = create_map(Lower, Upper, N),
% If a char is not in a..zA..z then show it as it is.
Cipher := [M.get(C,C) : C in String].
create_map(Lower,Upper, N) = M =>
M = new_map(),
Len = Lower.length,
foreach(I in 1..Len)
II = (N+I) mod Len,
if II == 0 then II := Len end, % Adjust for 1 based
M.put(Upper[I],Upper[II]),
M.put(Lower[I],Lower[II])
end.