0 Replies - 1323 Views - Last Post: 23 November 2009 - 12:13 PM

#1 erik.price   User is offline

  • D.I.C Lover
  • member icon

Reputation: 486
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Rotate 13

Posted 23 November 2009 - 12:13 PM

Description: Basic example of encryption (although it offers next to no security). Surprisingly easy compared to other languages. The rotate 13 function shifts all letters 13 places, and is undone by calling rotate 13 on the resulting string
def rot13(str) #str is the string to encrypt
       str.tr("A-Ma-mN-Zn-z", "N-Zn-zA-Ma-m")
       =begin 
       String#tr is declared as String#tr(from_string, to_string), where the letters in from_string 
       are substituted into to_string, so characters in the range of A-M are placed where characters of
       N-Z are in the original string, and so on
       =end
end


#example usage:
rot13("Hello World") # => "Uryyb Jbeyq"




Is This A Good Question/Topic? 0
  • +

Page 1 of 1