w3resource

Ruby String Exercises: Insert a string of length 2 to an another string where the first string will be in the middle of the second string

Ruby String: Exercise-2 with Solution

Write a Ruby program to insert a string of length 2 to an another string where the first string will be in the middle of the second string.

Ruby String Exercises: Insert a string of length 2 to an another string where the first string will be in the middle of the second string

Ruby Code:

def make_word(sstr, str)
    "#{sstr[0...sstr.length/2]}#{str}#{sstr[sstr.length/2..-1]}"
end
print make_word("Pyon", "th")
print "\n",make_word("Pyonz", "th")
print "\n",make_word("Pyonxz", "th")

Output:

Python
Pythonz
Pyothnxz

Flowchart:

Flowchart: Draw a string as bold or italic text

Ruby Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a Ruby program to draw a string as bold or italic text.
Next: Write a Ruby program to lower case, upper case and capitalizes all the words of a given string.

What is the difficulty level of this exercise?