I want to reverse all the words in a string such that
- Orders of the words should not be change
- Number of blank spaces in the words should be remain the same
For Ex:- When my string is "How Are You" it should return "woH erA uoY"
I tries something like following
def reverse_string(str)
arr = str.split(" ")
new_arr = arr.collect{|a| a.reverse}
new_arr.join(" ")
end
But it will not work for the strings which may have multiple blank spaces between the words.
1 Answer