21 lines
595 B
Plaintext
21 lines
595 B
Plaintext
local function caesar(s, k, e=true)
|
|
-- Parameters: s (string), k (key)
|
|
-- e (true=encode, false=decode)
|
|
if !e do k=-k end
|
|
local out = ""
|
|
for i = 1, #s do
|
|
local c = s[i]
|
|
if c:islower() then
|
|
out ..= string.char((c:byte() - 97 + k) % 26 + 97)
|
|
elseif c:isupper() then
|
|
out ..= string.char((c:byte() - 65 + k) % 26 + 65)
|
|
else
|
|
out ..= c
|
|
end
|
|
end
|
|
return out
|
|
end
|
|
|
|
print(caesar("The quick brown fox jumps over the lazy dog.", 3))
|
|
print(caesar("Wkh txlfn eurzq ira mxpsv ryhu wkh odcb grj.", 3, false))
|