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
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 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:
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?
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics