Ruby 3.6--Alternate code for practice


hello friends, I have solved Ruby string substitution problem,

<Below this line, add a link to the EXACT exercise that you are stuck at.>

<In what way does your code behave incorrectly? Include ALL error messages.>
Now I want to try the same problem with a different approach to practice.
I want to change “S” into “th” as well as “th” into “s”…I want to change this simultaneously.Can anybody tell me how to change two strings simultaneously…I am thinking of using “and” operator.Can we use operators for strings.


print "Pleathe enter a thtring: "
user_input = gets.chomp
#print user_in

if user_input.include?“s”


puts user_input.upcase!
print (“There is no in string”)

<do not remove the three backticks above>


the easy thing you can do is introduce an elsif, when user_input doesn’t include an s, the elsif can check if user_input includes a th and change it to s


Thanks for your response, But I want to change two strings simultaneously.
Any ideas in such scenario.


then you are really complicating matters for yourself, given changes s into th and then th into s will undo your action. Are you sure that is what you want?


Ok, I didn’t think this way.Thanks for heads up.I am still learning.


nothing is impossible, but we need to find a way to prevent this.

what we could do, is make a copy of the user inputted string:

test = user_input.dup

test is a terrible variable name, please think of something better.

now we have two variables, so on one we can substitute s with th and on the other th with s

then we need to merge the two strings, for which we can use a loop + if condition


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