Reputation: 102
How you capitalise the last alphabet of each word present in the string in ruby? For example:
Input String: the creator never dies Output String Must Be: thE creatoR neveR dieS
Note: Length of the string is not constant.
Upvotes: 0
Views: 116
Reputation: 5767
A quick and dirty way is:
(s.reverse.split(" ").each {|w| w.capitalize!}).join(" ").reverse
s
is your stringUpvotes: 1
Reputation: 46667
str.split.map do |word|
word[-1] = word[-1].upcase
word
end.join(' ')
That is - split the word at whitespace; form a new array of each word with the last character uppercased; join them back together
Upvotes: 1