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 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:
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?
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://w3resource.com/ruby-exercises/basic/ruby-basic-exercise-15.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics