Guest User

Untitled

a guest
May 9th, 2020
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.92 KB | None | 0 0
  1. def cesaer_cipher string, shift_number
  2.   alphabet_lower = %w[a b c d e f g h i j k l m n o p q r s t u v w x y z]
  3.   phrase_array =  string.chars
  4.   final_encode = []
  5.  
  6.   shifted_alphabet = alphabet_lower[-shift_number..-1]
  7.   0.upto(alphabet_lower.length - shift_number -1) do |index|
  8.      shifted_alphabet << alphabet_lower[index]
  9.   end
  10.  
  11.   0.upto(phrase_array.size - 1) do |letter|
  12.      0.upto(alphabet_lower.size - 1)  do |symbol|
  13.         if phrase_array[letter] == alphabet_lower[symbol]
  14.             final_encode.push(shifted_alphabet[symbol])
  15.             break
  16.         elsif phrase_array[letter] == alphabet_lower[symbol].capitalize
  17.             final_encode.push(shifted_alphabet[symbol].capitalize)
  18.             break
  19.         else
  20.             final_encode.push(phrase_array[symbol])
  21.             break
  22.         end
  23.       end
  24.   end
  25.   return  final_encode
  26. end
  27.  
  28. cipher = cesaer_cipher 'I am going to', 3
  29. puts cipher
Advertisement
Add Comment
Please, Sign In to add comment