Last active
April 26, 2019 10:39
-
-
Save LucasBarbara/63c56c72ac85672b2e3a3007c7e6c515 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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