I am creating a caesar cipher for The Odin Project's Ruby Programming course, I have my code to the point where I have one method that can take a single word and a shift value and returns the ciphered word using corresponding hash keys and values. And I have another method that takes a sentence and splits it into an array containing each separated word. What I would like to do is combine these two methods so that when you input a sentence, the words are split up into an array, then each part of the array is ciphered using the shift value, then the ciphered words from the array are printed back into sentence form.
Here is my code so far:
"a" => 1,
"b" => 2,
"c" => 3,
"d" => 4,
"e" => 5,
"f" => 6,
"g" => 7,
"h" => 8,
"i" => 9,
"j" => 10,
"k" => 11,
"l" => 12,
"m" => 13,
"n" => 14,
"o" => 15,
"p" => 16,
"q" => 17,
"r" => 18,
"s" => 19,
"t" => 20,
"u" => 21,
"v" => 22,
"w" => 23,
"x" => 24,
"y" => 25,
"z" => 26,
}```
```#multi letter caesar_cipher
def word_cipher(word, shift)
word.split(//).each {|letter| print @cipher.key(@cipher[letter]+ shift)}
end
> word_cipher("kittens", 2)
=> mkvvgpu
#split sentence string into an array of words
def sentence_array(sentence)
sentence.split.each { |word| print word.split }
end
>sentence_array("Look at all of these kittens")
=>["Look"]["at"]["all"]["of"]["these"]["kittens"]
And what I have for my the solution so far
def caesar_cipher(input, shift)
sentence_array(input) = words
words.split(//).each {|letter| print @cipher.key(@cipher[letter]+ shift)}
end
caesar_cipher("I love kittens", 2)
This is my first time posting on here so I'm sorry if I did a bad job explaining anything but any help would be appreciated!!
Thanks!
you have to slightly modify the methods:
#multi letter caesar_cipher
def word_cipher(word, shift)
output = ''
word.split(//).each {|letter| output << @cipher.key(@cipher[letter]+ shift)}
output
end
def sentence_array(sentence)
sentence.split
end
#multi letter caesar_cipher
def caesar_cipher(input, shift)
output = ""
words = sentence_array(input)
words.each do |word|
output << word_cipher(word.downcase, shift)
output << " " unless word == words.last
end
output.capitalize
end
puts caesar_cipher("I love kittens", 2)