-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsub_strings.rb
More file actions
46 lines (30 loc) · 951 Bytes
/
sub_strings.rb
File metadata and controls
46 lines (30 loc) · 951 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
def caesar_cipher(string, shift)
return_ord_array = []
cipher_shifted = []
final_array = []
# need to downcase string to convert to ordinal
string_array = string.downcase.split('')
#converate string to ord and push to return_ord_array
for i in string_array
return_ord_array.push(i.ord)
end
#logic to move letters that run past z with shift
for i in return_ord_array
if i < 97 || i > 122
cipher_shifted.push(i)
elsif i + shift > 122
x = i + shift
y = x - 122
cipher_shifted.push(96 + y)
else cipher_shifted.push(i + shift)
end
end
#recombine for final message
for i in cipher_shifted
final_array.push(i.chr)
end
puts " First combo string array changed to ord #{return_ord_array}"
puts " combined cipher ord numbers #{cipher_shifted} "
puts " Final message encryption #{final_array.join('')} "
end
caesar_cipher("What a string!", 5)