31
\$\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\$
3
  • 1
    \$\begingroup\$ Related. \$\endgroup\$ May 17 '17 at 15:02
  • 1
    \$\begingroup\$ Can we take the input as a list of words? (i.e. ['man', 'bites', 'dog']) \$\endgroup\$ May 17 '17 at 15:18
  • 3
    \$\begingroup\$ Can the output have trailing whitespace? \$\endgroup\$ May 17 '17 at 16:13

50 Answers 50

1
2
1
\$\begingroup\$

Pyth, 3 bytes

_cw

My first Pyth answer, one byte shorter than @notjagan's answer!

Explained:

 cw # Split the input by space (same as Python's string.split())
_   # Reverses the array
    # Pyth prints implicitly.
\$\endgroup\$
1
\$\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
\$\endgroup\$
1
\$\begingroup\$

Vyxal, 3 bytes

⌈ṘṄ

Try it Online!

\$\endgroup\$
0
\$\begingroup\$

TAESGL, 7 bytes

ĴS)Ř)ĴS

Interpreter

Explanation

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

Ruby, 23+1 = 24 bytes bytes

Uses the -p flag.

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

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Stacked, 17 bytes

' 'split rev' '#`

Try it online!

#` is "join".

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

Cheddar, 26 bytes

3 bytes thanks to Conor O'Brien

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

Try it online!

\$\endgroup\$
1
0
\$\begingroup\$

Scala, 37 bytes

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

Try it here.

\$\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

\$\endgroup\$
0
\$\begingroup\$

Ruby, 27 24 bytes

thanks to @manatwork

->a{a.split.reverse*" "}
\$\endgroup\$
1
  • \$\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

\$\endgroup\$
0
\$\begingroup\$

Clojure(script), 57 bytes

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

Lua, 66 42 bytes

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

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Vim, 49 42 bytes

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

Thank you to @manatwork for feedback

\$\endgroup\$
3
  • \$\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. :)

\$\endgroup\$
2
  • \$\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\$ May 18 '17 at 13:47
0
\$\begingroup\$

tcl, 18

puts [lreverse $S]

demo

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

PowerShell Core, 45 29 bytes

$args|%{$r=,$_+$r}
$r-join' '

Try it online!

-16 bytes thanks to mazzy!

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

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!

Another answer, 38 bytes:

s/^/:/
:
s/:(.*) (.*)/\2 :\1/
t
s/://

Try it online!

\$\endgroup\$
1
2

Your Answer

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