# Reversing of Words

Sona is in her house with her 10 year old daughter. She needs to go to school to bring back another child from school, as school is over at 2 pm. It's hot outside, so she wants to leave her younger child at home.

She gave a bunch of strings to her child to keep her busy while she is gone. She asked her to reverse the words in the string. There are lot of strings, so you need to help her daughter in solving this huge task.

So, given a string that contains words separated by single space, reverse the words in the string. You can assume that no leading or trailing spaces are there.

The string will only contain [a-zA-z ], so you don't need to handle punctuation.

You will be given a string as an input, and you should output a string.

### Sample Test Cases:

Input:
Man bites dog

Output:
dog bites Man

Input:
The quick brown fox jumps over the lazy dog

Output:
dog lazy the over jumps fox brown quick The

Input:
Hello world

Output:
world Hello


### Scoring

This is . Shortest answer in bytes wins.

• Related. – Martin Ender May 17 '17 at 15:02
• Can we take the input as a list of words? (i.e. ['man', 'bites', 'dog']) – Rɪᴋᴇʀ May 17 '17 at 15:18
• Can the output have trailing whitespace? – Digital Trauma May 17 '17 at 16:13

# Pyth, 3 bytes

_cw


Explained:

 cw # Split the input by space (same as Python's string.split())
_   # Reverses the array
# Pyth prints implicitly.


# Wren, 40 bytes

Fn.new{|a|a.split(" ")[-1..0].join(" ")}


Try it online!

## Explanation

Fn.new{|a|                             } // Anonymous function with the parameter a
a.split(" ")                   // Split on spaces
[-1..0]            // Reverse the array
.join(" ")  // Join the array with spaces


# Vyxal, 3 bytes

⌈ṘṄ


Try it Online!

# TAESGL, 7 bytes

ĴS)Ř)ĴS


Interpreter

Explanation

 ĴS)Ř)ĴS
AĴS)        implicit input "A" split at " "
Ř)      reversed
ĴS    joined with " "


# Ruby, 23+1 = 24 bytes bytes

Uses the -p flag.

$_=$_.split.reverse*' '


Try it online!

# Stacked, 17 bytes

' 'split rev' '#


Try it online!

# is "join".

# MATL, 5 bytes

YbPZc


Try it at MATL Online!

Explanation

    % Implicitly grab input as a string
Yb  % Split it on all of the spaces, yielding a cell array of words
P   % Flip this array
Zc  % Join these words back together with spaces
% Implicitly display the result


# Cheddar, 26 bytes

3 bytes thanks to Conor O'Brien

@.split(" ").rev.join(" ")


Try it online!

# Scala, 37 bytes

s=>s.split(" ").reverse.mkString(" ")


Try it here.

# jq, 27 characters

(23 characters code + 4 characters command line options)

./" "|reverse|join(" ")


Sample run:

bash-4.4$jq -Rr './" "|reverse|join(" ")' <<< 'Man bites dog' dog bites Man  On-line test # Ruby, 27 24 bytes thanks to @manatwork ->a{a.split.reverse*" "}  • Same with shorter syntax: ->a{a.split.reverse*" "} – manatwork May 18 '17 at 11:25 # Swift, 116 bytes var r:(String)->String={return$0.characters.split(separator:" ").map(String.init).reversed().joined(separator:" ")}


You can try it here

# Clojure(script), 57 bytes

#(apply str(interpose" "(reverse(re-seq #"[a-zA-Z]+"%))))


# Lua, 66 42 bytes

for i=#...,1,-1 do
io.write(arg[i],' ')end


Try it online!

# Vim, 49 42 bytes

:s/.*/\=join(reverse(split(submatch(0))))/


Thank you to @manatwork for feedback

• Does this require a specific version? Vim 8.0 gives me “E486: Pattern not found: \%V.*\%V.” – manatwork May 18 '17 at 11:23
• BTW, it seems to work fine without those \%V things: :s/.*/\=join(reverse(split(submatch(0))))/. – manatwork May 18 '17 at 11:33
• @manatwork it works for vim 8 only I did it with visual select aka %V for no practical reason.. my bad :D Thank you for the feed back I will update the answer – Hex May 22 '17 at 9:16

# AWK, 30 28 bytes

{for(;++i<=NF;)s=$i" "s}$0=s


Try it online!

Thanks, manatwork, for catching my needless fluff. :)

• Not specified by the challenge, but hopefully there is no need to handle empty input, so {for(;++i<=NF;)s=$i" "s}$0=s. – manatwork May 18 '17 at 8:53
• Trying to remember what was going on 21 hours ago... failing. Good catch @manatwork . – Robert Benson May 18 '17 at 13:47

puts [lreverse $S]  # demo # tcl, 11 lreverse "" ^ | Put the string you want to test here between the ""  Can be tested on http://www.tutorialspoint.com/execute_tcl_online.php starting the tcl interpreter by typing tclsh. Then type lreverse "Man bites dog"  % lreverse "The quick brown fox jumps over the lazy dog"  % lreverse "Hello world"  # PowerShell Core, 45 29 bytes $args|%{$r=,$_+$r}$r-join' '


Try it online!

-16 bytes thanks to mazzy!

• Try it online!? because there is the rule "The string will only contain [a-zA-z ]" – mazzy Jun 24 at 5:34
• – mazzy Jun 24 at 5:37

# GNU sed 4.2.2, 34 bytes

Score includes +1 for -r passed to GNU sed.

:
s/(\w*) ([^:]*)/\2:\1/
t
y/:/ /


Try it online!

s/^/:/