w3resource

Ruby Basic Exercises: Create a new string from a given string with the last character added at the front and back of the given string

Ruby Basic: Exercise-24 with Solution

Write a Ruby program to create a new string from a given string with the last character added at the front and back of the given string. The lenght of the given string must be 1 or more.

Ruby Basic Exercises: Create a new string from a given string with the last character added at the front and back of the given string

Ruby Code:

def temp(str)
    len = str.length() - 1;
	return (str.split('')[len] + str + str.split('')[len]);
end

print temp("abc"),"\n"
print temp("abcd"),"\n"
print temp("java")

Output:

cabcc
dabcdd
ajavaa

Flowchart:

Flowchart: Create a new string from a given string with the last character added at the front and back of the given string

Ruby Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a Ruby program to print "Ruby Basic Exercises" 9 times.
Next: Write a Ruby program to check two temperatures and return true if one is less than 0 and the other is greater than 100.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.