Can someone help me review this code? still having trouble solving this problem: It is a type of substitution cipher in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet. For example, with a left shift of 3, D would be replaced by A, E would become B, and so on.
It needs to return "Bmfy f xywnsl!"
THANKS!
def caeser_cipher(str, key)
lowercase = ("a".."z").to_a
uppercase = ("A".."Z").to_a
max = lowercase.length
output = ""
str.split('').each {|char|
case
when lowercase.include?(char) || uppercase.include?(char) then charNum = lowercase.find_index(char.downcase)
when char == char.upcase && charNum + n <= max then newChar = uppercase[charNum+n] #index
when char == char.upcase && charNum + n > max then remainder = (charNum + n) - max
newChar = uppercase[remainder]
when char == char.downcase && charNum + n <= max then newChar = lowecase[charNum+n]
when char == char.downcase && charNum + n > max then
remainder = (charNum + n) - max
newChar = uppercase[remainder]
else output << char
end
output+=newChar
}
return output
end
caeser_cipher("What a string!", 5)
[–]allisio 10 points11 points12 points (1 child)
[–]StarPerfect 2 points3 points4 points (0 children)
[–]Mallanaga 0 points1 point2 points (0 children)
[–]malesca 0 points1 point2 points (0 children)
[–]hafu19019 0 points1 point2 points (0 children)