From: Christian Date: 2008-03-17T10:32:59+09:00 Subject: Re: Cipher ------=_Part_5331_11499180.1205717588747 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline input = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ !@#$%^&*()_+-={}|[]\;\':",./<>?`~' input.tr! "A-Za-z", "N-ZA-Mn-za-m" => "nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM !@\#$%^&*()_+-={}|[]\\;':\",./<>?`~" On Mon, Mar 17, 2008 at 11:16 AM, Thomas Wieczorek wrote: > On Mon, Mar 17, 2008 at 1:50 AM, Max Zhou wrote: > > I am trying to make a program that takes what you input, and encrypts it > by turning a to p, b to q, z to b, etc. It moves the letter 2 spaces to the > left (a to c) and moves it down (c to p). (See below.) Even though you can > just say that a will be n, how do you seperate each letter and turn it into > a string? Please put it in terms that a Ruby beginner would understand. > > abcdefghijklm > > nopqrstuvwxyz > > > > You can split a string with the String#split method: > irb > > "Foobar".split(//) > => ["F", "o", "o", "b", "a", "r"] > > You can get the ASCII value of a character either with ?character: > > ?F > => 70 > > If you want to loop the splitted string, try eval(has anyone a better > solution?): > > "Foobar".split(//).each { |c| puts eval("?#{c}") } > > You can use Fixnum#chr to turn an ASCII value in a character: > > 65.chr #=> "A" > > You're trying to implement the ROT13 or ROTx algorithm. A naive > approach, still buggy might look like that: > plaintext = "Hello" > encrypted = "" > plaintext.split(//).each { |c| i=eval("?#{c}"); encrypted << (i+13).chr } > puts encrypted #=> Uryy| > > As you see, the encrypted string contains a |(vertical line) and it > also chokes on spaces. I leave the rest to you. Ask again if you're > stuck. > > Regards, Thomas > > -- "Every child has many wishes. Some include a wallet, two chicks and a cigar, but that's another story." ------=_Part_5331_11499180.1205717588747--