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

Cyoce
3,37126 silver badges29 bronze badges
asked May 17, 2017 at 15:00
\$\endgroup\$
3
  • 1
    \$\begingroup\$ Related. \$\endgroup\$ Commented May 17, 2017 at 15:02
  • 1
    \$\begingroup\$ Can we take the input as a list of words? (i.e. ['man', 'bites', 'dog']) \$\endgroup\$ Commented May 17, 2017 at 15:18
  • 3
    \$\begingroup\$ Can the output have trailing whitespace? \$\endgroup\$ Commented May 17, 2017 at 16:13

55 Answers 55

1
2
11
\$\begingroup\$

Retina, 7 bytes

O$^`\w+

Try it online!

Match all words (\w+) sort them with sort key empty string (O$) which means they won't get sorted at all, and then reverse their order (^).

answered May 17, 2017 at 15:02
\$\endgroup\$
2
  • \$\begingroup\$ Never used Retina but why do you need the 0$? Can't you just reverse it? \$\endgroup\$ Commented May 23, 2017 at 23:19
  • \$\begingroup\$ @RandomUser sort mode (O) is currently the only mode that has this reverse option. \$\endgroup\$ Commented May 24, 2017 at 0:02
10
\$\begingroup\$

Haskell, 21 bytes

unwords.reverse.words

Try it online!

answered May 17, 2017 at 15:08
\$\endgroup\$
10
\$\begingroup\$

Python 3, 29 bytes

print(*input().split()[::-1])

Try it online!

answered May 17, 2017 at 15:30
\$\endgroup\$
3
  • \$\begingroup\$ What does the * do? \$\endgroup\$ Commented May 28, 2017 at 23:38
  • \$\begingroup\$ @OldBunny2800 unpack the list \$\endgroup\$ Commented May 29, 2017 at 2:41
  • \$\begingroup\$ Ahh. Makes sense. \$\endgroup\$ Commented May 29, 2017 at 3:03
8
\$\begingroup\$

Bash + common Linux utilities, 21

printf "1ドル "|tac -s\ 

Leaves a trailing space in the output string - not sure if that's OK or not.

answered May 17, 2017 at 16:15
\$\endgroup\$
2
  • 3
    \$\begingroup\$ Congrats on 50k!! Your turn today :-) \$\endgroup\$ Commented May 18, 2017 at 17:02
  • \$\begingroup\$ @LuisMendo Thanks! \$\endgroup\$ Commented May 18, 2017 at 17:02
7
\$\begingroup\$

JavaScript (ES6), 31 bytes

s=>s.split` `.reverse().join` `

Try it

f=
s=>s.split` `.reverse().join` `
o.innerText=f(i.value="Man bites dog")
oninput=_=>o.innerText=f(i.value)
<input id=i><pre id=o>

answered May 17, 2017 at 15:03
\$\endgroup\$
3
  • 1
    \$\begingroup\$ It's answers like this, that is essentially the same as my C# answer that makes me hate C# for golfing. All the extra fluff in my answer nearly doubles the byte count... +1 \$\endgroup\$ Commented May 17, 2017 at 15:59
  • \$\begingroup\$ Side note: In C# if you pass nothing to Split it splits on whitespace by default, can you do the same here? \$\endgroup\$ Commented May 17, 2017 at 16:00
  • 1
    \$\begingroup\$ Unfortunately not, @TheLethalCoder, if you don't supply a string/regex to split in JS, it will either split on each individual character or create an array with a single element containing the original string, depending on the syntax used. \$\endgroup\$ Commented May 17, 2017 at 18:25
7
\$\begingroup\$

Brachylog, 6 bytes

ṇ1↔~ṇ1

Try it online!

Explanation

ṇ1 Split on spaces
 ↔ Reverse
 ~ṇ1 Join with spaces

Note that both "split on spaces" and "join wth spaces" use the same built-in, that is ṇ1, just used in different "directions".

answered May 17, 2017 at 16:15
\$\endgroup\$
6
\$\begingroup\$

Jelly, 3 bytes

ḲṚK

Try it online!

Explanation:

Ḳ Splits the input at spaces
Ṛ Reverses the array
K Joins the array, using spaces
answered May 17, 2017 at 15:03
\$\endgroup\$
1
6
\$\begingroup\$

R, 19 bytes

cat(rev(scan(,'')))

reads the string from stdin. By default, scan reads tokens separated by spaces/newlines, so it reads the words in as a vector. rev reverses, and cat prints the elements with spaces.

Try it online!

answered May 17, 2017 at 15:32
\$\endgroup\$
4
\$\begingroup\$

C#, 58 bytes

using System.Linq;s=>string.Join(" ",s.Split().Reverse());
answered May 17, 2017 at 15:51
\$\endgroup\$
4
\$\begingroup\$

C, (削除) 54 (削除ここまで) 48 bytes

Using arguments as input, 48 bytes

main(c,v)char**v;{while(--c)printf("%s ",v[c]);}

Try Online

> ./a.out man bites dog

Using pointers, 84 bytes

f(char*s){char*t=s;while(*t)t++;while(t-s){while(*t>32)t--;*t=0;printf("%s ",t+1);}}

Use

main(){ f("man bites dog"); }
answered May 18, 2017 at 11:37
\$\endgroup\$
2
  • \$\begingroup\$ That's sneaky, using the shell to do the word-splitting for you! Clever abuse of the rules. \$\endgroup\$ Commented Jul 9, 2023 at 12:34
  • \$\begingroup\$ Second one only works if your compiler puts strings into writable storage and has non-printing character immediately preceding. It segfaults here. \$\endgroup\$ Commented Jul 9, 2023 at 13:26
3
\$\begingroup\$

05AB1E, 4 bytes

#Rðý

Note: Will only work for 2 or more words. +1 byte if this is not OK.

Try it online!

answered May 17, 2017 at 15:05
\$\endgroup\$
3
  • \$\begingroup\$ I see unicode, is it reallly 4 bytes? \$\endgroup\$ Commented May 18, 2017 at 4:38
  • \$\begingroup\$ Yes, 05AB1E uses a custom codepage \$\endgroup\$ Commented May 18, 2017 at 7:34
  • \$\begingroup\$ #R¸» alternate 4-byte solution :P. \$\endgroup\$ Commented Jun 28, 2017 at 19:17
3
\$\begingroup\$

GNU Make, 62 bytes

$(if 1,ドル$(call 0,ドル$(wordlist 2,$(words 1ドル),1ドル)) $(word 1,1ドル),)
answered May 17, 2017 at 15:26
\$\endgroup\$
3
\$\begingroup\$

brainfuck, 74 bytes

,[>++++[<-------->-],]<[>++++[->--------<]+>[[<]>[+>]<]<-[<]>[.>]<[[-]<]<]

Try it online!

This code creates the number -32 in two different places, but that seems to be fewer bytes than trying to maintain a single -32.

Explanation

,[ input first character
 >++++[<-------->-] subtract 32 from current character (so space becomes zero)
,] repeat for all characters in input
< go to last character of last word
[ while there are more words to display:
 >++++[->--------<] create -32 two cells right of last letter
 +> increment trailing space cell (1 right of last letter) so the next loop works
 [[<]>[+>]<] add 32 to all cells in word and trailing space cell
 <- subtract the previously added 1 from the trailing space
 [<]> move pointer to beginning of word
 [.>]< output word (with trailing space)
 [[-]<] erase word
 < move to last character of previous word
]
answered May 18, 2017 at 1:49
\$\endgroup\$
3
\$\begingroup\$

Cubix, 48 bytes

Almost gave up on this one, but finally got there.

oU_;SW;@?ABu>):tS-?;0円$q^s.$;;<$|1osU!(;;...<#(1

Try it online!

This maps onto a cube with a side length of three as follows

 o U _
 ; S W
 ; @ ?
A B u > ) : t S - ? ; \
0 $ q ^ s . $ ; ; < $ |
1 o s U ! ( ; ; . . . <
 # ( 1
 . . .
 . . .

The general steps are:

  • Get all input A and reverse B stack
  • Move the negative q to the bottom, add a counter 0 to the stack. bit of jumping around in here.
  • Find space/end loop, also puts stack in correct print order.
    • Increment counter ) and fetch the counter item from the stack t
    • Is it a space or EOI S-?
    • Repeat if not
  • Print word loop
    • Decrement counter (
    • Exit loop if counter !U is 0
    • Swap s counter with character on stack
    • Print o character and pop it from the stack ;
    • Repeat loop
  • Get the length of the stack # and decrement (
  • Check ? if 0 and exit @ if it is 0
  • Otherwise print a space So clean up ;; and go back to the first loop.

I've skipped a number of superfluous steps, but you can see it Step By Step

answered May 18, 2017 at 3:06
\$\endgroup\$
3
\$\begingroup\$

Japt, (削除) 11 (削除ここまで) (削除) 10 (削除ここまで) (削除) 7 (削除ここまで) 4 bytes

My first attempt at Japt.

 ̧w ̧

Try it online


Explanation

 :Implicit input of string U
 ̧ :Split on <space>
w :Reverse
 ̧ :Join with <space>

Please share your Japt tips here.

answered May 18, 2017 at 7:15
\$\endgroup\$
6
  • 2
    \$\begingroup\$ Thanks for using Japt :-) You can use ¸ in place of qS , which should save you three bytes here. (See the "Unicode shortcuts" section of the interpreter docs) \$\endgroup\$ Commented May 18, 2017 at 12:36
  • \$\begingroup\$ Nice! you can save a byte if you use the -S flag. \$\endgroup\$ Commented May 18, 2017 at 14:40
  • \$\begingroup\$ I count 2 bytes, @obarakon. Unless the flag is included in the byte count, in which case that would be 4 byes, no? \$\endgroup\$ Commented May 18, 2017 at 14:42
  • \$\begingroup\$ @Shaggy Each flag counts as one byte. So -S would be +1 onto your total byte count. \$\endgroup\$ Commented May 18, 2017 at 14:43
  • \$\begingroup\$ Ah, I see. Is that a PPCG thing or a Japt thing? \$\endgroup\$ Commented May 18, 2017 at 14:44
2
\$\begingroup\$

Python 2, 34 bytes

lambda s:' '.join(s.split()[::-1])

Try it online!

answered May 17, 2017 at 15:02
\$\endgroup\$
1
  • \$\begingroup\$ Out-golfed. >_> Well... the other one only works in Python 3... \$\endgroup\$ Commented May 17, 2017 at 15:47
2
\$\begingroup\$

Pyth, 4 bytes

jd_c

Try it online!

answered May 17, 2017 at 15:04
\$\endgroup\$
2
\$\begingroup\$

PHP, 47 Bytes

<?=join(" ",array_reverse(explode(" ",$argn)));

Try it online!

answered May 17, 2017 at 15:22
\$\endgroup\$
2
\$\begingroup\$

k, 9 bytes

" "/|" "\

Try it in your browser of the web variety!

 " "\ /split on spaces
 | /reverse
" "/ /join with spaces
answered May 18, 2017 at 0:09
\$\endgroup\$
2
\$\begingroup\$

J-uby, 23 bytes

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

Explanation

:split # split by spaces
| # then
:reverse # reverse 
| # then
~:*&' ' # join with spaces
answered May 19, 2017 at 21:34
\$\endgroup\$
2
\$\begingroup\$

Mathematica, 35 bytes

StringRiffle@Reverse@StringSplit@#&

Try it online!

answered May 18, 2017 at 0:31
\$\endgroup\$
3
  • \$\begingroup\$ StringSplit[#] splits on whitespace automatically, so you don't need to specify the " ". \$\endgroup\$ Commented May 20, 2017 at 0:34
  • 2
    \$\begingroup\$ correct! -5 bytes! \$\endgroup\$ Commented May 20, 2017 at 0:39
  • \$\begingroup\$ Ooh, and I think you can save another byte using function composition: StringRiffle@*Reverse@*StringSplit (call it like StringRiffle@*Reverse@*StringSplit@"hello world") \$\endgroup\$ Commented May 20, 2017 at 0:56
2
\$\begingroup\$

Vim, 20 bytes

:s/ /\r/g|g/^/m0<cr>vGJ

This is shorter than the other vim answer.

Try it online!

answered May 23, 2017 at 16:19
\$\endgroup\$
2
\$\begingroup\$

Röda, (削除) 27 (削除ここまで) 25 bytes

2 bytes saved thanks to @fergusq

{[[split()|reverse]&" "]}

Try it online!

This function takes input from the input stream.

Explanation (outdated)

{[[(_/" ")()|reverse]&" "]} /* Anonymous function */
 (_/" ") /* Pull a value from the stream and split it on spaces */
 () /* Push all the values in the resulting array to the stream */
 |reverse /* And reverse it */
 [ ] /* Wrap the result into an array*/
 &" " /* Concatenate each of the strings in the array with a space */
 [ ] /* And push this result to the output stream */
answered May 17, 2017 at 19:43
\$\endgroup\$
1
  • \$\begingroup\$ split uses space as the default separator, so split() is shorter than (_/" ")(). \$\endgroup\$ Commented May 19, 2017 at 16:45
2
\$\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.
answered May 28, 2017 at 23:51
\$\endgroup\$
2
\$\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
answered Dec 18, 2019 at 14:22
\$\endgroup\$
2
\$\begingroup\$

Zsh, 13 bytes

echo ${(Oa)@}

Try it online!

answered Jul 9, 2023 at 10:11
\$\endgroup\$
1
\$\begingroup\$

Ohm, 4 bytes

z]Qù

Try it online!

Explanation

z Split the input on spaces.
 ] Dump it onto the stack.
 Q Reverse the stack.
 ù Join the stack with spaces. Implicit output.
answered May 17, 2017 at 15:16
\$\endgroup\$
1
\$\begingroup\$

CJam, 7 bytes

qS/W%S*

Try it online!

Explanation

q e# Read input
 S/ e# Split on spaces
 W% e# Reverse
 S* e# Join with spaces
answered May 17, 2017 at 15:08
\$\endgroup\$
1
\$\begingroup\$

TAESGL, 7 bytes

ĴS)Ř)ĴS

Interpreter

Explanation

 ĴS)Ř)ĴS
AĴS) implicit input "A" split at " "
 Ř) reversed
 ĴS joined with " "
answered May 17, 2017 at 15:08
\$\endgroup\$
1
\$\begingroup\$

J, 6 bytes

|.&.;:

Try it online! This is reverse (|.) under (&.) words (;:). That is, split sentence into words, reverse it, and join the sentence again.

answered May 18, 2017 at 0:10
\$\endgroup\$
1
2

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.