practice-problems-1/comments/19-caesar-cipher.rb
# Write a method that takes in an integer `offset` and a string.
# Produce a new string, where each letter is shifted by `offset`. You
# may assume that the string contains only lowercase letters and
# spaces.
#
# When shifting "z" by three letters, wrap around to the front of the
# alphabet to produce the letter "c".
#
# You'll want to use String's `ord` method and Integer's `chr` method.
# `ord` converts a letter to an ASCII number code. `chr` converts an
# ASCII number code to a letter.
#
# You may look at the ASCII printable characters chart:
#
#     http://en.wikipedia.org/wiki/ASCII#ASCII_printable_characters
#
# Notice that the letter 'a' has code 97, 'b' has code 98, etc., up to
# 'z' having code 122.
#
# You may also want to use the `%` modulo operation to handle wrapping
# of "z" to the front of the alphabet.
#
# Difficulty: hard. Because this problem relies on outside
# information, we would not give it to you on the timed challenge. :-)
Write a method that takes in an integer `offset` and a string.
Produce a new string, where each letter is shifted by `offset`. You
may assume that the string contains only lowercase letters and
spaces.

When shifting "z" by three letters, wrap around to the front of the
alphabet to produce the letter "c".

You'll want to use String's `ord` method and Integer's `chr` method.
`ord` converts a letter to an ASCII number code. `chr` converts an
ASCII number code to a letter.

You may look at the ASCII printable characters chart.





You may also want to use the `%` modulo operation to handle wrapping
of "z" to the front of the alphabet.

hard. Because this problem relies on outside
information, we would not give it to you on the timed challenge.
Write a method that takes in an integer ` offset ` and a string . Produce a new string where each letter is shifted by ` offset ` . You may assume that the string contains only lowercase letters and spaces . When shifting " z " by three letters wrap around to the front of the alphabet to produce the letter " c " . You ' ll want to use String ' s ` ord ` method and Integer ' s ` chr ` method . ` ord ` converts a letter to an ASCII number code . ` chr ` converts an ASCII number code to a letter . You may look at the ASCII printable characters chart . You may also want to use the ` % ` modulo operation to handle wrapping of " z " to the front of the alphabet . hard . Because this problem relies on outside information we would not give it to you on the timed challenge .
def caesar_cipher(offset, string)
  alpha = ("a".."z").to_a + ("a".."z").to_a          
  newString = ""                                     
  string = string.split(" ")
  string.each do |i|                                 
    i.chars.each do |j|                              
      newString << alpha[alpha.index(j).to_i+offset] 
   end
     newString += " "                                
  end
     return newString.strip                          
end
def caesar_cipher ( offset , string ) alpha = ( "a" .. "z" ) . to_a + ( "a" .. "z" ) . to_a newString = "" string = string . split ( " " ) string . each do | i | i . chars . each do | j | newString << alpha [ alpha . index ( j ) . to_i + offset ] end newString += " " end return newString . strip end
Write a method that takes in an integer `offset` and a string.
Write a method that takes in an integer ` offset ` and a string .
#so it "loops"  back to the beginning #answer #loop through array of string(s) #loop through each letter in the string #shift the letter by the offset #add an empty space so you it's not all one string. This is for conditions than > 1 word.  #remove whitespace at the end of the string
offset
<not_specific>
string
<not_specific>