I so decided to make a palindrome program, but I did so by checking a string inputted by a user. In order to do this properly, I wanted to strip away the capitalization, spaces, and punctuation. I managed to get everything but the punctuation part of it working. Every time I try with a string like "Madam, I'm Adam" the program crashes. I am very new to Ruby, and I have only learned my knowledge through the Codecademy website. And I also using the editor provided to run my code. Every time I run it like:
puts "Enter a string!"
user_input = gets.chomp
user_input.downcase!
user_input = user_input.gsub(/[^0-9a-z ]/i, '')
if user_input.include?(" ")
user_input.gsub!(/ /, "")
end
if user_input == user_input.reverse
print "Is a pallindrome"
else
print "Is not a pallindrome"
end
It crashes. But if I run it like:
puts "Enter a string!"
user_input = gets.chomp
user_input.downcase!
if user_input.include?(" ")
user_input.gsub!(/ /, "")
end
if user_input == user_input.reverse
print "Is a pallindrome"
else
print "Is not a pallindrome"
end
It works. What a I doing wrong here? Why does my program always crash when I attempt to take away the punctuation?