Jump to content

Reverse words in a string: Difference between revisions

no edit summary
(→‎{{header|Perl 6}}: explicit stringification to avoid printing parenthesis)
No edit summary
Line 1,798:
print();
});</lang>
 
=={{header|Swift}}==
<lang swift>import Foundation
 
// convenience extension for better clarity
extension String {
var lines: [String] {
get {
return self.componentsSeparatedByCharactersInSet(NSCharacterSet.newlineCharacterSet())
}
}
var words: [String] {
get {
return self.componentsSeparatedByCharactersInSet(NSCharacterSet.whitespaceCharacterSet())
}
}
}
 
let input = "---------- Ice and Fire ------------\n\nfire, in end will world the say Some\nice. in say Some\ndesire of tasted I've what From\nfire. favor who those with hold I\n\n... elided paragraph last ...\n\nFrost Robert -----------------------\n"
 
let output = input.lines.map { $0.words.reverse().joinWithSeparator(" ") }.joinWithSeparator("\n")
 
print(output)</lang>
{{out}}
<pre>
------------ Fire and Ice ----------
 
Some say the world will end in fire,
Some say in ice.
From what I've tasted of desire
I hold with those who favor fire.
 
... last paragraph elided ...
 
----------------------- Robert Frost
</pre>
 
=={{header|Tcl}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.