"C" words that sound like "s" - what should be different?


#1

this seemed logical ...

if user_input.include? "s"
user_input.gsub!(/s/, "th")
elsif user_input.include? "ce"
user_input.gsub!(/ce/, "th")
elsif user_input.include? "ci"
user_input.gsub!(/ci/, "th")
else

... yet the output doesn't change the "ce" or "ci" to "th"s...
What gives?


#2

Your problem is that your elsif statements will always be exclusive, so only one will be used every time.

You should try putting all your conditions in one statement.


#3

The bonus of an else if statement is that ONLY ONE of the if's will be chosen. But if the if before the if you want to be if'd gets called then that if will be the only if statement that runs it's statement. SIMPLE!!!

In lamens terms just change the else if's to their own if statments :smiley:


#4

this is what I used : P seems a tad long though. Cant help but feel like there's shorthand
that would work just as efficiently.
also, I added "i" to the end of my subbed outputs to maintain the sound.
thoughts?

if user_input.include? "s"
user_input.gsub!(/s/, "th")
end

if user_input.include? "ce"
user_input.gsub!(/ce/, "the")
end

if user_input.include? "ci"
user_input.gsub!(/ci/, "thi")
end

if user_input.include? "cy"
user_input.gsub!(/cy/, "thi")
end


#5

Yes, one solution could be something like this:

if user_input.include? "s" || (user_input.include? "ce") || (user_input.include? "ci")
    user_input.gsub!(/s/, "th")
    user_input.gsub!(/ce/, "the")
    user_input.gsub!(/ci/, "thi")

#6

I've done this task in this way:

print "Please type any word:"
user_input = gets.chomp
if user_input.empty?
print "You forgot to type something:"
elsif user_input.include? ("s" || "S") || ("ci" || "Ci") || ("ce" || "Ce")
user_input.gsub!(/s/, "th")
user_input.gsub!(/S/, "th")
user_input.gsub!(/ci/, "thi")
user_input.gsub!(/Ci/, "thi")
user_input.gsub!(/ce/, "the")
user_input.gsub!(/Ce/, "the")
puts "Duffy says: #{user_input}"
else
print "No Duffy Duck for #{user_input}"
end

Looks like this solution covers all suggestions for project improvement)


#7

Very helpful, thankyou!
Francisco.


#8

It also works fine this way:

print "String please: "
user_input = gets.chomp
user_input.downcase!

if user_input.empty?
puts "you forgot to type something"
elsif user_input.include? "s"
user_input.gsub!(/s/, 'th')
elsif user_input.include? "ce"
user_input.gsub!(/ce/, 'the')
elsif user_input.include? "ci"
user_input.gsub!(/ci/, 'thi')
else
puts "oops!! There's no \"s\" in it"
end

puts "you write: #{user_input}"