Skip to content

Instantly share code, notes, and snippets.

@gingeleski
Last active August 29, 2015 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gingeleski/8b56dc7230348a1708a2 to your computer and use it in GitHub Desktop.
Save gingeleski/8b56dc7230348a1708a2 to your computer and use it in GitHub Desktop.
Takes an input string and an integer shift factor, performs a Caesar cipher.
# Takes an input string and an integer shift factor, performs
# a Caesar cipher.
def caesar_cipher(input,shift_factor)
message = input.scan(/./)
cipher = Array.new
for x in message
y = x[0].to_i
z = y + shift_factor
if y >= 65 && y <= 90
if z < 65 || z > 90
cipher << ((z % 90) + 64).chr
else
cipher << z.chr
end
elsif y >= 97 && y <= 122
if z < 97 || z > 122
cipher << ((z % 122) + 96).chr
else
cipher << z.chr
end
else
cipher << x[0]
end
end
return cipher.join
end
# EXAMPLE
puts caesar_cipher("ZzZz",4)
# Caesar cipher shifted 4 results in "DdDd"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment