this is my first try just changing the input string into array and substituting each character with case statement. you can try this script at labs.codeacademy.computs "-- ROT 13 encrypter--"def encrypter(x)z = case xwhen "a" then "n"when "b" then "o"when "c" then "p"when "d" then "q"when "e" then "r"when "f" then "s"when "g" then "t"when "h" then "u"when "i" then "v"when "j" then "w"when "k" then "x"when "l" then "y"when "m" then "z"when "n" then "a"when "o" then "b"when "p" then "c"when "q" then "d"when "r" then "e"when "s" then "f"when "t" then "g"when "u" then "h"when "v" then "i"when "w" then "j"when "x" then "k"when "y" then "l"when "z" then "m"else "ERROR"endreturn zendputs "Enter word: "word = gets.chompwordarr = []donearr = []wordarr = word.split("")wordarr.each {|y| donearr << encrypter(y) }puts "Result: "puts donearr.join

but i think i can do better, why not i try taking the value of character and adding/subtracting 13 instead. but then it was troublesome.

after some googling (or cheating) there is a simple way to do this... using the tr method

puts "Enter word: "word = gets.chompword.tr!("abcdefghijklmnopqrstuvwxyz","nopqrstuvwxyzabcdefghijklm")puts word

to make things a bit smarter, use method

def rot13(word)

word.tr!("abcdefghijklmnopqrstuvwxyz","nopqrstuvwxyzabcdefghijklm")

end

puts "Enter word: "

enterword = gets.chomp

puts rot13(enterword)

## No comments:

Post a Comment