Created
February 20, 2014 02:54
-
-
Save jstuckey/9106255 to your computer and use it in GitHub Desktop.
Check if word is a palindrome
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
def is_palindrome(string_to_check) | |
if string_to_check.length == 0 | |
return false | |
end | |
the_start = 0 | |
the_end = string_to_check.length - 1 | |
palindrome_found = true | |
while the_start <= the_end | |
left_char = string_to_check[the_start] | |
right_char = string_to_check[the_end] | |
if left_char != right_char | |
palindrome_found = false | |
break | |
end | |
the_start = the_start + 1 | |
the_end = the_end - 1 | |
end | |
return palindrome_found | |
end | |
words = %W"radar racecar buzz abba aba aa bbb test aaabbb abcdefghhgfedcba abcdefghxzhgfedcba" | |
words.each do |word| | |
puts "Is #{word} a palindrome? #{is_palindrome(word) ? 'Yes' : 'No'}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment