w3resource

Ruby Basic Exercises: Create a new string from a given string where the first and last characters have been exchanged

Ruby Basic: Exercise-15 with Solution

Write a Ruby program to create a new string from a given string where the first and last characters have been exchanged.

Ruby Basic Exercises: Create a new string from a given string where the first and last characters have been exchanged

Ruby Code:

def front_back(txt)
    txt[-1] << txt[1...-1] << txt[0]
end
print front_back("Python"),"\n"
print front_back("Java"),"\n"

Output:

nythoP
aavJ

Flowchart:

Flowchart: Create a new string from a given string where the first and last characters have been exchanged

Ruby Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a Ruby program which accept the radius of the sphere as input and compute the volume.
Next: Write a Ruby program to test whether you are minor (Consider a child unless he or she is less than 18 years old.) or not.

What is the difficulty level of this exercise?



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/ruby-exercises/basic/ruby-basic-exercise-15.php