Ruby 3.6--Alternate code for practice


#1

hello friends, I have solved Ruby string substitution problem,

<Below this line, add a link to the EXACT exercise that you are stuck at.>
https://www.codecademy.com/courses/learn-ruby/lessons/thith-meanth-war/exercises/returning-the-final-stringer-thtring?action=lesson_resume

<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
user_in=user_input.downcase!
#print user_in

if user_input.include?“s”

user_input.gsub!(/s/,“th”)

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

<do not remove the three backticks above>

#2

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


#3

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


#4

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?


#5

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


#6

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


#7

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