fork download
  1. def caesar_cipher(str, key)
  2. str1 = str.split(//)
  3. arr = []
  4. str1.each do |ch|
  5. temp = ch
  6. if (/\w/.match(temp))
  7. key.times do
  8. if (/[^zZ]/.match(temp))
  9. temp.next!
  10. elsif (/[z]/.match(temp))
  11. temp = "a"
  12. elsif (/[Z]/.match(temp))
  13. temp = "A"
  14. end
  15. end
  16. end
  17. arr << temp
  18. end
  19. arr.join
  20. end
  21.  
  22. puts caesar_cipher("What a string!", 5)
Success #stdin #stdout 0.05s 9656KB
stdin
Standard input is empty
stdout
Bmfy f xywnsl!