How to preserve case of characters when using Caesar Cipher - ruby

I have a Caesar Cipher script in Ruby that is working but it returns the string as all upper-case letters instead of preserving the cases of the original string.
I could use capitalize to make it look good enough but I would like a more concrete way of preserving the cases.
Here is the script:
BASE_ORD = 'A'.ord
def caesar_cipher(phrase, key)
cipher = phrase.gsub(/[a-z]/i) do |c|
orig_pos = c.upcase.ord - BASE_ORD
new_pos = (orig_pos + key) % 26
(new_pos + BASE_ORD).chr
end
puts cipher
end
caesar_cipher("What a string!", 5)
Any help or insight would be appreciated.

The simplest solution, given your existing code, is to check whether the character is uppercase or lowercase and set base_ord accordingly. Since the lowercase letters come after the uppercase letters in UTF-8 (as in ASCII), we can just test letter >= 'a', e.g.:
base_ord = (letter >= 'a' ? 'a' : 'A').ord
Here's the whole method with this change (you no longer need the BASE_ORD constant):
def caesar_cipher(phrase, key)
phrase.gsub(/[a-z]/i) do |letter|
base_ord = (letter >= 'a' ? 'a' : 'A').ord
orig_pos = letter.ord - base_ord
new_pos = (orig_pos + key) % 26
(new_pos + base_ord).chr
end
end
puts caesar_cipher("What a string!", 5) # => Bmfy f xywnsl!
Edit
Amadan makes a good point about using String#tr. Here's a somewhat more concise implementation:
ALPHABET = "aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ"
# Or if you want to be fancy: ALPHABET = (?a..?z).flat_map {|c| [ c, c.upcase ] }.join
def caesar_cipher(phrase, key)
to_alphabet = ALPHABET.dup
to_alphabet << to_alphabet.slice!(0, key * 2)
phrase.tr(ALPHABET, to_alphabet)
end
puts caesar_cipher("What a string!", 5) # => Bmfy f xywnsl!

As said in comments, tr is easier to use for Caesar Cypher (once you prepare the two alphabets), and should also be much faster:
class CaesarCypher
def initialize(key, alphabet=nil)
#from_alphabet = alphabet || (?a..?z).to_a.join
#to_alphabet = #from_alphabet[key..-1] + #from_alphabet[0...key]
#from_alphabet += #from_alphabet.upcase
#to_alphabet += #to_alphabet.upcase
end
def encode(str)
str.tr(#from_alphabet, #to_alphabet)
end
def encode!(str)
str.tr!(#from_alphabet, #to_alphabet)
end
def decode(str)
str.tr(#to_alphabet, #from_alphabet)
end
def decode(str)
str.tr!(#to_alphabet, #from_alphabet)
end
end
cc = CaesarCypher.new(1)
puts cc.encode("Caesar, huh?")
puts cc.decode("Dbftbs, ivi?")

Related

having trouble with .next method making a casear cipher

I'm not worried about what happens if my key will go past Z right now, or capital letters. All I want is my outcome to be something like. text=abc key=2 and it print "cde". Where am I going wrong?
puts "What would you like to cipher?"
text = gets.chomp
puts " what number key would you like?"
key = gets.chomp.to_i
def casear_cipher(text,key)
ciphered_text = []
text.chars.each do |letter|
ciphered_text = letter
ciphered_text = ciphered_text.next
end
end
puts casear_cipher(text,key)
You're not using the key yet, so it will always just do abc -> bcd. If you're really not concerned about "Z" going to "AA", you can try this:
def cipher(text, key)
text.chars.map { |c| (c.ord + key).chr }.join
end
Since 'Z'.next => 'AA' and 'z'.next #=> 'aa', we can use [-1] to select the last letter.
In the code below we perform next! on each character n times using the times method. next! modifies the character whereas next does not.
def casear_cipher(text, n)
text.chars.map do |c| n.times { c.next! }
c[-1]
end.join
end
p casear_cipher('abc',2) #=> "cde"
p casear_cipher('xyz',2) #=> "zab"
p casear_cipher('ZEBRA',2) #=> "BGDTC"
More information about these methods can be found at http://www.ruby-doc.org/core-2.4.1/

I am creating a caesar cipher in Ruby but the caesar function never finishes

Here is my code:
def caesar(string, shift_factor)
alphabet = Array("a".."z")
new_alph = alphabet.rotate(shift_factor)
new_str = []
new_str = string.downcase.split("")
new_str.each do |i|
print i
if !alphabet.include?(i)
new_str.push(i)
else
equals = alphabet.index(i)
new_str.push(new_alph[equals])
end
end
end
caesar("What a string!", 0)
print new_str.join.capitalize!
The code just keeps on looping and I am not sure how to go about stopping it.
You need a different variable for storing the result string. How about this:
def caesar(string, shift_factor)
alphabet = Array("a".."z")
new_alph = alphabet.rotate(shift_factor)
new_str = string.downcase.split("")
caesar_string = []
new_str.each do |i|
if !alphabet.include?(i)
caesar_string.push(i)
else
equals = alphabet.index(i)
caesar_string.push(new_alph[equals])
end
end
caesar_string
end
caesar_string = caesar("What a string!", 0)
print caesar_string.join.capitalize!
You're iterating over new_str and in each iteration you're pushing another object onto the array so the loop will never end.
In your loop, if you instead replace the character at the index, then you should get the result you're looking for.
def caesar(string, shift_factor)
alphabet = Array("a".."z")
new_alph = alphabet.rotate(shift_factor)
new_str = string.downcase.split("")
new_str.each_with_index do |letter, i|
if !alphabet.include?(letter)
new_str[i] = letter
else
equals = alphabet.index(letter)
new_str[i] = new_alph[equals]
end
end
end
Just to add an example that in Ruby there is always more than one way of doing things:
def caesar(string, shift_factor)
alphabet = ('a'..'z').to_a
string.downcase.tr(alphabet.join, alphabet.rotate(shift_factor).join)
end

Why does Ruby convert `string.chr` values to `?`?

I want to shift the chr value of the characters in a string by a certain amount. This code:
def encrypt2(word, num)
cipher = word.split('').map{|e|
e = e.ord + num
e = e.chr
}.join.reverse
puts cipher
end
encrypt2('hellozz', 10)
outputs: ??yvvor. It seems that after 126.chr, it only returns question marks.
e = e.ord + num will produce non-ASCII characters.
You can make use the String#next method to get the next ASCII character for a given character. However, 'z'.next will be 'aa' due to the carry created, hence, in such cases one has to pick the first character of that result.
So, you could write:
num.times { e = e.next[0]}
def encrypt2(word, num)
word.chars.map {|e| num.times {e = e.next[0]}; e}.join.reverse
end
puts encrypt2('hellozz', 10)
#=> "jjyvvor"
puts encrypt2("naïve", 1)
#=> fwðbo

Creating A Caesar Cipher in Ruby, getting an error

I am attempting to create a Caesar Cipher in Ruby for my computer science class. My friend was able to create part of the code:
def cipher(word, n)
new_word = ""
word.each_char do |i|
n.times do
if(i == "z")
i = "a"
next
elsif(i == "Z")
i = "A"
next
end
i.next!
i == "%" ? i = " " : ""
end
new_word += i
end
puts new_word
end
cipher("phrase", 5)
Where the last line is where you would put the phrase you want to scramble, and the number is how much you want to scramble it by. One of the requirements is that we use gets.chomp to specify a phrase and amount to scramble by without editing the .rb file itself. So I came up with this:
puts "What would you like to scramble?"
word = gets.chomp
puts "How much would you like to scramble that?"
n = gets.chomp
def cipher(word, n)
new_word = ""
word.each_char do |i|
n.times do
if(i == "z")
i = "a"
next
elsif(i == "Z")
i = "A"
next
end
i.next!
i == "%" ? i = " " : ""
end
new_word += i
end
puts new_word
end
cipher(word, n)
And I get the following error outputed when run in Terminal:
some.rb:10:in `block in cipher': undefined method `times' for "5":String (NoMethodError)
from some.rb:9:in `each_char'
from some.rb:9:in `cipher'
from some.rb:26:in `<main>'
If someone could help me figure out what I'm doing wrong, that would help me out a lot.
gets.chomp returns a string
word = gets.chomp
So word is a string, as expected, but then you call gets.chomp again, this time to get number of scrabbles that should be applied to the string. So n is a string as well.
n = gets.chomp
When you call the times method on n it's not defined, because it only makes sense on integers. The solution is to convert n to an integer. This should work:
n = gets.chomp.to_i
Update
Documentation on the to_i method on String instances: http://ruby-doc.org/core-2.0.0/String.html#method-i-to_i
Call .to_i on n.
You need to convert that string you got from the user's input into a number before you can run .times on it. .to_i does this for you.
Example:
http://progzoo.net/wiki/Ruby:Convert_a_String_to_a_Number
Did this a while ago, the requirements were only lowercase ASCII alphabet letters, hope you get the general idea to do it your way:
def encrypt(msg, key)
msg.downcase.split("").each_with_index do |char, i|
next if msg[i] == " "
msg[i] = (msg[i].ord + key) > 122 ? (((msg[i].ord + key) % 123) + 97).chr : (msg[i].ord + key).chr
end
msg
end
def decrypt(msg, key)
msg.downcase.split("").each_with_index do |char, i|
next if msg[i] == " "
msg[i] = (msg[i].ord - key) < 97 ? (123 - (97 - (msg[i].ord - key))).chr : (msg[i].ord - key).chr
end
msg
end
gets.chomp return a string, you must convert it to a number in order to call .times method. Change this line n = gets.chomp by n = gets.chomp.to_i

Ruby Pig Latin Multiple Arguments

Hi I'm trying to write code for to convert strings to pig latin
def translate(str)
alpha = ('a'..'z').to_a
vowels = %w[a e i o u]
consonants = alpha - vowels
if vowels.include?(str[0])
str + 'ay'
elsif str[0..1] == 'qu'
str[2..-1]+'quay'
elsif consonants.include?(str[0]) && str[1..2]=='qu'
str[3..-1]+str[0..2]+'ay'
elsif consonants.include?(str[0]) && consonants.include?(str[1]) && consonants.include?(str[2])
str[3..-1] + str[0..2] + 'ay'
elsif consonants.include?(str[0]) && consonants.include?(str[1])
str[2..-1] + str[0..1] + 'ay'
elsif consonants.include?(str[0])
str[1..-1] + str[0] + 'ay'
elsif str[0..1] == 'qu'
str[2..-1]+'quay'
else
return str
end
end
This code works perfect for converting one word strings, for example: translate("monkey").
What i'm trying to do is make it possible for this code to accept multiple words as well (within the same string)...following the above criteria for converting into pig latin, example:
translate("please help") => "easeplay elphay"
thanks much!
Since you already know how to translate a single word why not just split up the task into two methods:
def translate(str)
str.split.map { |word| translate_word(word) }.join
end
def translate_word(str)
# Your old translate code here
end
What I would do for this is:
use the #split method to make your str variable into an array of words (or 1 word if its only 1 word).
afterwards you can use the array#each method to iterate through each array index.
i.e.
str = "hello"
str = str.split(" ") # str now equals ["hello"]
for multiple variables:
str = "hello world"
str- str.split(" ") #now equals ["hello", "world"]
then you can use the .each method:
str.each do |<variable name you want to use>|
<how you want to manipulate the array>
end
for the pig latin program you could do:
str.each do|element|
if vowels.include?(element)
<do whatever you want here>
elsif
<do whatever>
else
<do whatver>
end
end
this will iterate through each element in the array and translate it (if there is only one element it will still work)

Resources