Created
January 16, 2024 08:17
-
-
Save Mr-Fox-h/3c841938110d8fb169a5c5385d2e02e6 to your computer and use it in GitHub Desktop.
caesar-cipher for the odin project
This file contains hidden or 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
require "pry-byebug" | |
def caesar_cipher(string_text, num) | |
content_split = string_text.downcase.split("") | |
content_ascii = [] | |
content_split.each do |target| | |
target.each_byte { |char| content_ascii << char } | |
end | |
ascii_plus = [] | |
content_ascii.each do |target| | |
if target == 32 || target == 33 | |
ascii_plus << target | |
else | |
if target + num > 122 | |
target = target + num | |
min = target - 123 | |
target = 97 + min | |
ascii_plus << target | |
else | |
ascii_plus << target + num | |
end | |
end | |
end | |
whole_array = ascii_plus.map { |c| c.chr } | |
whole_string = whole_array.join("") | |
whole_string[0] = whole_string[0].upcase | |
whole_string | |
end | |
puts caesar_cipher("What a string!", 5) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment