From a string, I am trying to replace each character with another character, 13 letters ahead in the alphabet. For example, "ABC" would return "NOP".
However, when I get to the end of the alphabet, I can't find a way to loop the index, so that it goes from index [-1] to [0] again and onwards. For example - When I type in "XYZ" it returns "", but it should return "KLM".
Can anyone think of a solution to this?
def rot13(string)
alphabet = ("a".."z").to_a
big_alphabet = ("A".."Z").to_a
result = []
split_string = string.chars
alphabet.each_with_index do |item,index|
if string.include?(alphabet[index])
result << alphabet[index+13]
elsif string.include?(big_alphabet[index])
result << big_alphabet[index+13]
end
end
return result.join
end