'S's won't change if it isn't the first letter in the word



If the letter 's' or 'S' are not the first letting in a word on the user_input string, they will not convert to 'th' and the else part of the statement runs. shouldn't the user_input.include? ("S" || "s") statement check the entire string for 's' or 'S' at any point in the whole string? I wasn't sure if maybe this was just a glitch, but wanted to check to make sure my code was correct.

print "Give me a string:"

while user_input.empty?
    puts "You gotta enter something buddy:"

if user_input.include? ("S" || "s")

    puts "there are no Ss here"

puts "You are saying it wrong its: #{user_input}"


This expression:

"S" || "s"

evaluates to:


Therefore, this expression:

user_input.include? ("S" || "s")

is equivalent to:

user_input.include? ("S")

Here's an example showing you that your code does indeed do its thing even when the 'S' is not the first character:

Give me a string:yummy bananaS
You are saying it wrong its: yummy bananaTh

And besides, if there's no S or s in the text, the substitution has no effect, so why check first? Just do the substitution either way.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.