RosettaCodeData/Task/Caesar-cipher/Eiffel/caesar-cipher.e

54 lines
1.5 KiB
Text
Raw Permalink Normal View History

2023-07-01 11:58:00 -04:00
class
2026-02-01 16:33:20 -08:00
APPLICATION
2023-07-01 11:58:00 -04:00
inherit
2026-02-01 16:33:20 -08:00
ARGUMENTS
2023-07-01 11:58:00 -04:00
create
2026-02-01 16:33:20 -08:00
make
2023-07-01 11:58:00 -04:00
feature {NONE} -- Initialization
2026-02-01 16:33:20 -08:00
make
-- Run application.
local
s: STRING_32
do
s := "The tiny tiger totally taunted the tall Till."
print ("%NString to encode: " + s)
print ("%NEncoded string: " + encode (s, 12))
print ("%NDecoded string (after encoding and decoding): " + decode (encode (s, 12), 12))
end
2023-07-01 11:58:00 -04:00
2026-02-01 16:33:20 -08:00
feature -- Basic operations
2023-07-01 11:58:00 -04:00
2026-02-01 16:33:20 -08:00
decode (to_be_decoded: STRING_32; offset: INTEGER): STRING_32
-- Decode `to be decoded' according to `offset'.
do
Result := encode (to_be_decoded, 26 - offset)
end
2023-07-01 11:58:00 -04:00
2026-02-01 16:33:20 -08:00
encode (to_be_encoded: STRING_32; offset: INTEGER): STRING_32
-- Encode `to be encoded' according to `offset'.
local
l_offset: INTEGER
l_char_code: INTEGER
do
create Result.make_empty
l_offset := (offset \\ 26) + 26
across to_be_encoded as tbe loop
if tbe.item.is_alpha then
if tbe.item.is_upper then
l_char_code := ('A').code + (tbe.item.code - ('A').code + l_offset) \\ 26
Result.append_character (l_char_code.to_character_32)
else
l_char_code := ('a').code + (tbe.item.code - ('a').code + l_offset) \\ 26
Result.append_character (l_char_code.to_character_32)
end
else
Result.append_character (tbe.item)
end
end
end
2023-07-01 11:58:00 -04:00
end