RosettaCodeData/Task/Caesar-cipher/GAP/caesar-cipher.gap

27 lines
680 B
Text
Raw Permalink Normal View History

2023-07-01 11:58:00 -04:00
CaesarCipher := function(s, n)
2026-02-01 16:33:20 -08:00
local r, c, i, lower, upper;
lower := "abcdefghijklmnopqrstuvwxyz";
upper := "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
r := "";
for c in s do
i := Position(lower, c);
if i <> fail then
Add(r, lower[RemInt(i + n - 1, 26) + 1]);
else
i := Position(upper, c);
if i <> fail then
Add(r, upper[RemInt(i + n - 1, 26) + 1]);
else
Add(r, c);
fi;
fi;
od;
return r;
2023-07-01 11:58:00 -04:00
end;
CaesarCipher("IBM", 25);
# "HAL"
CaesarCipher("Vgg cphvi wzdibn vmz wjmi amzz viy zlpvg di ydbidot viy mdbcon.", 5);
# "All human beings are born free and equal in dignity and rights."