Capital i


#1

https://www.codecademy.com/courses/learn-ruby/lessons/thith-meanth-war/exercises/returning-the-final-stringer-thtring?action=lesson_resume&link_content_target=interstitial_undefined

In the final few lessons of “Thith meanth war,” you are supposed to put a string that removes the S’s and replaces them with Th. This is easy. What is annoying, is if I have an “i” in the middle of the phrase, I cannot capitalize it. I tried this:

print "Say sumpin! "
user_input = gets.chomp
user_input.downcase!
if user_input.include? "s"
user_input.gsub!(/s/, “th”)
else
puts "You didn’t type any S’s idiot!"
end
if user_input.include? "i"
user_input.gsub!(/i/, “I”)
end
user_input.capitalize!
puts user_input

However, none of the i’s are replaced with I. I realize, even if this did work, it would replace i’s in the middle of words as well. Is there a way to replace only single, lower-case i’s with a capital I?
Sorry for the lack of indentions. I typed it properly, it just auto pushes it back on the forums for some reason.


#2

Cleaned it up a bit:

print "Say sumpin! "
user_input = gets.chomp
user_input.downcase!
if user_input.include? "i"
user_input.gsub!(/i/, “I”)
end
if user_input.include? "s"
user_input.gsub!(/s/, “th”)
user_input.capitalize!
puts user_input
else
puts "You didn’t type any S’s idiot!"
end


#3

Why and when should i be capitalized?
I’m a little confused about what’s stopping you too. To replace one occurrence, find the location of the first one, and if there is such a location, do the replacement there

Most forums apply formatting to posts, they’re not plain text. There are buttons in the editor for including code.


#4

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