Skip to content

Instantly share code, notes, and snippets.

@jstuckey
Created February 20, 2014 02:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jstuckey/9106255 to your computer and use it in GitHub Desktop.
Save jstuckey/9106255 to your computer and use it in GitHub Desktop.
Check if word is a palindrome
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