30
\$\begingroup\$

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.

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

47 Answers 47

1
2
0
\$\begingroup\$

TAESGL, 7 bytes

ĴS)Ř)ĴS

Interpreter

Explanation

 ĴS)Ř)ĴS
AĴS)        implicit input "A" split at " "
    Ř)      reversed
      ĴS    joined with " "
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Ruby, 23+1 = 24 bytes bytes

Uses the -p flag.

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

Try it online!

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Stacked, 17 bytes

' 'split rev' '#`

Try it online!

#` is "join".

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

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
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Cheddar, 26 bytes

3 bytes thanks to Conor O'Brien

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

Try it online!

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Scala, 37 bytes

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

Try it here.

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

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

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Ruby, 27 24 bytes

thanks to @manatwork

->a{a.split.reverse*" "}
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Same with shorter syntax: ->a{a.split.reverse*" "} \$\endgroup\$ – manatwork May 18 '17 at 11:25
0
\$\begingroup\$

Swift, 116 bytes

var r:(String)->String={return $0.characters.split(separator:" ").map(String.init).reversed().joined(separator:" ")}

You can try it here

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Clojure(script), 57 bytes

#(apply str(interpose" "(reverse(re-seq #"[a-zA-Z]+"%))))
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Lua, 66 42 bytes

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

Try it online!

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

J-uby, 23 bytes

:split|:reverse|~:*&' '

Explanation

:split   # split by spaces
|        # then
:reverse # reverse 
|        # then
~:*&' '  # join with spaces
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Vim, 49 42 bytes

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

Thank you to @manatwork for feedback

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Does this require a specific version? Vim 8.0 gives me “E486: Pattern not found: \%V.*\%V.” \$\endgroup\$ – manatwork May 18 '17 at 11:23
  • \$\begingroup\$ BTW, it seems to work fine without those \%V things: :s/.*/\=join(reverse(split(submatch(0))))/. \$\endgroup\$ – manatwork May 18 '17 at 11:33
  • \$\begingroup\$ @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 \$\endgroup\$ – Hex May 22 '17 at 9:16
0
\$\begingroup\$

AWK, 30 28 bytes

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

Try it online!

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

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

tcl, 18

puts [lreverse $S]

demo

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

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"
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

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
| improve this answer | |
\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy