fork download
  1. def caesar_cipher(str, key)
  2. str1 = str.chars
  3. arr = []
  4. str1.each do |ch|
  5. temp = ch
  6. if /\w/ =~ temp
  7. key.times do
  8. if /[^zZ]/ =~ temp
  9. temp.next!
  10. elsif /[z]/ =~ temp
  11. temp = 'a'
  12. elsif /[Z]/ =~ 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)
  23.  
Success #stdin #stdout 0.05s 9704KB
stdin
Standard input is empty
stdout
Bmfy f xywnsl!