I’m trying to look for a specific character in an array but this character is being entered by the user.
I first order the array and then ask the user to enter a specific character and then I should see if that character exists in any of the words that the array has
For some reason, if when checking for the existence of the character, I “hard code” a character, it works, but it doesn’t work if I try to look for the character that the user has entered…
list = [ 'Mom' , 'Dad' , 'Brother' , 'Sister' ]
puts ("Enter the character you would like to find");
character = gets
for i in 0..(list.length - 1)
if (list[i].include?(#{character}))
puts ("Character #{character} found in the word #{list[i]}");
end
Thanks a lot!
It is because
getsadds a\nto the end of the string. Usegets.chomp!so you can get rid of the last char.