Created
August 29, 2012 19:32
-
-
Save rvandervort/3517648 to your computer and use it in GitHub Desktop.
Palindrome checker in ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class String | |
def palindrome? | |
is_p = true | |
ch = self.gsub(/ /,"").chars.to_a | |
compares = ch.length / 2 | |
back_index = ch.length - 1 | |
compares.times do |i| | |
break unless is_p = (ch[i] == ch[back_index]) | |
back_index -= 1 | |
end | |
is_p | |
end | |
end | |
puts "rover".palindrome? # false | |
puts "roger".palindrome? # false | |
puts "radar".palindrome? # true | |
puts "madam im adam".palindrome? # true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment