Reverse a string word by word

From CodeCodex

Related content:

Reverses a string word by word, as opposed to character by character.

Implementations[edit]

C#[edit]

private static string reverseWords(string str) {
     string[] words = str.Split(' ');
     Array.Reverse(words);
     return String.Join(" ", words);
}

Erlang[edit]

reverseWords(S) ->
    string:join( lists:reverse( string:tokens(S, " ") ), " " ).

Haskell[edit]

reverseWords = unwords . reverse . words

Perl 5[edit]

 sub reverseWords {
     join ' ', reverse split(' ', shift)
 }

Perl 6[edit]

 sub reverseWords(Str $s) returns Str {
     $s.comb.reverse.join(' ')
 }

Python[edit]

def reverseWords(s):
    return ' '.join(reversed(s.split(' ')))

OR

def reverseWords(s):
    return ' '.join(s.split(' ')[::-1])

Ruby[edit]

def reverseWords(s)
  s.split.reverse.join(' ')
end

Tcl[edit]

package require struct::list
proc reverseWords s {return [struct::list reverse [split $s]]}