Monday, September 30, 2013

Palindrome in Ruby

A palindrome is a word, phrase, number, or other sequence of symbols or elements, whose meaning may be interpreted the same way in either forward or reverse direction. Examples: "Amore, Roma.", "A man, a plan, a canal: Panama." or "No “x” in “Nixon.”"

The palindrome code in ruby is very short also similar to other OOP languages. Check my code below.

#!/usr/bin/env ruby

print "Enter string: "

str = gets

strnew = str.downcase.scan(/\w/)

if strnew == strnew.reverse

        puts strnew.to_s << " is a palindrome"

end