Skip to content

Instantly share code, notes, and snippets.

@LucasBarbara
Last active April 26, 2019 10:39
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 LucasBarbara/63c56c72ac85672b2e3a3007c7e6c515 to your computer and use it in GitHub Desktop.
Save LucasBarbara/63c56c72ac85672b2e3a3007c7e6c515 to your computer and use it in GitHub Desktop.
puts "What is the string to be cyphered?"
string = gets.chomp
puts "Number of positions to shuffle:"
n = gets.chomp.to_i
def caesar_cypher(string, n)
new_string = ''
string.each_char do |c|
code = c.ord
# If lower case
if (code <= 122 && code >=97)
code = ((code - 97 + n) % 26) + 97
# If upper case
elsif (code <= 90 && code >= 65)
code = ((code - 65 + n) % 26) + 65
end
c = code.chr
new_string += c
end
new_string
end
puts "Cyphered string: " + caesar_cypher(string, n)
# "a".ord = 97
# "z".ord = 122
# "A".ord = 65
# "Z".ord = 90
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment