monicao
3/8/2013 - 8:39 PM

Palindrome problem

Palindrome problem

# Returns true if sentence is a palindrome.
# racecar   ---> true
# hello     ---> false
# aba aba   ---> true
# Never odd or even ---> true
def palindrome?(sentence)
  # downcase the sentence
  downcased_sentence = sentence.downcase

  # remove all the spaces in the sentence
  sentence_without_spaces = downcased_sentence.delete(' ')

  puts "Sentence: #{sentence_without_spaces}"

  # reverse the word
  reversed = sentence_without_spaces.reverse
  
  # compare the reversed word with the non-reversed word.
  puts "Reversed: #{reversed}"
  if sentence_without_spaces == reversed
    return true
  end
  return false
  # if they are equal return true
end


puts palindrome?("racecar")

puts palindrome?("hello")

puts palindrome?("Never odd or even")