Files
ivy/doc/sample/CaesarCipher.im

65 lines
888 B
Plaintext
Raw Permalink Normal View History

2024-11-02 15:11:00 +00:00
package net.doorstuck.test
cout put:'Encode a string with a Caesar cipher.'
message = ''
while true do
cout print:'Message to encode: '
cout flush
cin get:message
if message != '' then
break
end
end
2025-11-07 10:07:44 +00:00
shift-width = 0
2024-11-02 15:11:00 +00:00
while true do
cout print:'Shift size: '
cout flush
2025-11-07 10:07:44 +00:00
shift-str = cin read-line.
2024-11-02 15:11:00 +00:00
2025-11-07 10:07:44 +00:00
if shift-str == '' then
2024-11-02 15:11:00 +00:00
continue
end
try
2025-11-07 10:07:44 +00:00
shift-width = Int parse:shift-str
catch ($err:number-format, err)
2024-11-02 15:11:00 +00:00
continue
end
break
end
2025-11-07 10:07:44 +00:00
encoded-message = ''
2024-11-02 15:11:00 +00:00
for c in message do
2025-11-07 10:07:44 +00:00
i = c to-ordinal
2024-11-02 15:11:00 +00:00
sub = 0
if i >= 65 && i <= 90 then
sub = 65
elif i >= 97 && i <= 122 then
sub = 97
else
continue
end
2025-11-07 10:07:44 +00:00
2024-11-02 15:11:00 +00:00
i -= sub
2025-11-07 10:07:44 +00:00
i += shift-width
2024-11-02 15:11:00 +00:00
if i >= 26 then
i -= 26
end
2025-11-07 10:07:44 +00:00
c2 = i to-char
encoded-message += c2
2024-11-02 15:11:00 +00:00
end
2025-11-07 10:07:44 +00:00
cout put:encoded-message