You need to split
your word into the individual characters. Then you need to convert the characters to ints with ord
so you can do arithmetic. The arithmetic operation is to add an offset relative to 'a', modulo 26, so that the result maps to a different offset from 'a' which will result in a new character. Change the result back to character with chr
, and join the characters back together to form a string.
Here's one possible implementation which accommodates both upper and lower case letters:
def shift_char(c, base, offset)
(((c.ord - base) + offset) % 26 + base).chr
end
def cipher(s, offset)
s.chars.map do |c|
case c
when 'a'..'z'
shift_char(c, 'a'.ord, offset)
when 'A'..'Z'
shift_char(c, 'A'.ord, offset)
else
c
end
end.join
end
cipher_text = cipher('Now is the time for all good men...', 13)
p cipher_text # "Abj vf gur gvzr sbe nyy tbbq zra..."
original_text = cipher(cipher_text, 13)
p original_text # "Now is the time for all good men..."