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 code-golf. Shortest answer in bytes wins.
55 Answers 55
Retina, 7 bytes
O$^`\w+
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 (^).
-
\$\begingroup\$ Never used Retina but why do you need the
0$? Can't you just reverse it? \$\endgroup\$2017年05月23日 23:19:44 +00:00Commented May 23, 2017 at 23:19 -
\$\begingroup\$ @RandomUser sort mode (
O) is currently the only mode that has this reverse option. \$\endgroup\$Martin Ender– Martin Ender2017年05月24日 00:02:25 +00:00Commented May 24, 2017 at 0:02
-
\$\begingroup\$ What does the
*do? \$\endgroup\$AAM111– AAM1112017年05月28日 23:38:09 +00:00Commented May 28, 2017 at 23:38 -
\$\begingroup\$ @OldBunny2800 unpack the list \$\endgroup\$Rod– Rod2017年05月29日 02:41:50 +00:00Commented May 29, 2017 at 2:41
-
\$\begingroup\$ Ahh. Makes sense. \$\endgroup\$AAM111– AAM1112017年05月29日 03:03:36 +00:00Commented May 29, 2017 at 3:03
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.
-
3\$\begingroup\$ Congrats on 50k!! Your turn today :-) \$\endgroup\$Luis Mendo– Luis Mendo2017年05月18日 17:02:17 +00:00Commented May 18, 2017 at 17:02
-
\$\begingroup\$ @LuisMendo Thanks! \$\endgroup\$Digital Trauma– Digital Trauma2017年05月18日 17:02:36 +00:00Commented May 18, 2017 at 17:02
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>
-
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\$TheLethalCoder– TheLethalCoder2017年05月17日 15:59:51 +00:00Commented May 17, 2017 at 15:59
-
\$\begingroup\$ Side note: In C# if you pass nothing to
Splitit splits on whitespace by default, can you do the same here? \$\endgroup\$TheLethalCoder– TheLethalCoder2017年05月17日 16:00:26 +00:00Commented May 17, 2017 at 16:00 -
1\$\begingroup\$ Unfortunately not, @TheLethalCoder, if you don't supply a string/regex to
splitin 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\$Shaggy– Shaggy2017年05月17日 18:25:19 +00:00Commented May 17, 2017 at 18:25
Brachylog, 6 bytes
ṇ1↔~ṇ1
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".
Jelly, 3 bytes
ḲṚK
Explanation:
Ḳ Splits the input at spaces
Ṛ Reverses the array
K Joins the array, using spaces
-
5\$\begingroup\$ en.wikipedia.org/wiki/Krk \$\endgroup\$QBrute– QBrute2017年05月18日 06:29:41 +00:00Commented May 18, 2017 at 6:29
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.
C#, 58 bytes
using System.Linq;s=>string.Join(" ",s.Split().Reverse());
C, (削除) 54 (削除ここまで) 48 bytes
Using arguments as input, 48 bytes
main(c,v)char**v;{while(--c)printf("%s ",v[c]);}
> ./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"); }
-
\$\begingroup\$ That's sneaky, using the shell to do the word-splitting for you! Clever abuse of the rules. \$\endgroup\$Toby Speight– Toby Speight2023年07月09日 12:34:38 +00:00Commented 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\$Toby Speight– Toby Speight2023年07月09日 13:26:16 +00:00Commented Jul 9, 2023 at 13:26
-
\$\begingroup\$ I see unicode, is it reallly 4 bytes? \$\endgroup\$val - disappointed in SE– val - disappointed in SE2017年05月18日 04:38:20 +00:00Commented May 18, 2017 at 4:38
-
\$\begingroup\$ Yes, 05AB1E uses a custom codepage \$\endgroup\$kalsowerus– kalsowerus2017年05月18日 07:34:00 +00:00Commented May 18, 2017 at 7:34
-
\$\begingroup\$
#R¸»alternate 4-byte solution :P. \$\endgroup\$Magic Octopus Urn– Magic Octopus Urn2017年06月28日 19:17:55 +00:00Commented Jun 28, 2017 at 19:17
GNU Make, 62 bytes
$(if 1,ドル$(call 0,ドル$(wordlist 2,$(words 1ドル),1ドル)) $(word 1,1ドル),)
brainfuck, 74 bytes
,[>++++[<-------->-],]<[>++++[->--------<]+>[[<]>[+>]<]<-[<]>[.>]<[[-]<]<]
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
]
Cubix, 48 bytes
Almost gave up on this one, but finally got there.
oU_;SW;@?ABu>):tS-?;0円$q^s.$;;<$|1osU!(;;...<#(1
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
Aand reverseBstack - Move the negative
qto the bottom, add a counter0to 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 stackt - Is it a space or EOI
S-? - Repeat if not
- Increment counter
- Print word loop
- Decrement counter
( - Exit loop if counter
!Uis 0 - Swap
scounter with character on stack - Print
ocharacter and pop it from the stack; - Repeat loop
- Decrement counter
- Get the length of the stack
#and decrement( - Check
?if 0 and exit@if it is 0 - Otherwise print a space
Soclean up;;and go back to the first loop.
I've skipped a number of superfluous steps, but you can see it Step By Step
Japt, (削除) 11 (削除ここまで) (削除) 10 (削除ここまで) (削除) 7 (削除ここまで) 4 bytes
My first attempt at Japt.
̧w ̧
- Saved 3 bytes thanks to ETHproductions
Explanation
:Implicit input of string U
̧ :Split on <space>
w :Reverse
̧ :Join with <space>
Please share your Japt tips here.
-
2\$\begingroup\$ Thanks for using Japt :-) You can use
¸in place ofqS, which should save you three bytes here. (See the "Unicode shortcuts" section of the interpreter docs) \$\endgroup\$ETHproductions– ETHproductions2017年05月18日 12:36:44 +00:00Commented May 18, 2017 at 12:36 -
-
\$\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\$Shaggy– Shaggy2017年05月18日 14:42:21 +00:00Commented May 18, 2017 at 14:42
-
\$\begingroup\$ @Shaggy Each flag counts as one byte. So
-Swould be +1 onto your total byte count. \$\endgroup\$Oliver– Oliver2017年05月18日 14:43:53 +00:00Commented May 18, 2017 at 14:43 -
\$\begingroup\$ Ah, I see. Is that a PPCG thing or a Japt thing? \$\endgroup\$Shaggy– Shaggy2017年05月18日 14:44:32 +00:00Commented May 18, 2017 at 14:44
-
\$\begingroup\$ Out-golfed. >_> Well... the other one only works in Python 3... \$\endgroup\$totallyhuman– totallyhuman2017年05月17日 15:47:02 +00:00Commented May 17, 2017 at 15:47
k, 9 bytes
" "/|" "\
Try it in your browser of the web variety!
" "\ /split on spaces
| /reverse
" "/ /join with spaces
J-uby, 23 bytes
:split|:reverse|~:*&' '
Explanation
:split # split by spaces
| # then
:reverse # reverse
| # then
~:*&' ' # join with spaces
-
\$\begingroup\$
StringSplit[#]splits on whitespace automatically, so you don't need to specify the" ". \$\endgroup\$Not a tree– Not a tree2017年05月20日 00:34:33 +00:00Commented May 20, 2017 at 0:34 -
2\$\begingroup\$ correct! -5 bytes! \$\endgroup\$ZaMoC– ZaMoC2017年05月20日 00:39:36 +00:00Commented May 20, 2017 at 0:39
-
\$\begingroup\$ Ooh, and I think you can save another byte using function composition:
StringRiffle@*Reverse@*StringSplit(call it likeStringRiffle@*Reverse@*StringSplit@"hello world") \$\endgroup\$Not a tree– Not a tree2017年05月20日 00:56:40 +00:00Commented May 20, 2017 at 0:56
Röda, (削除) 27 (削除ここまで) 25 bytes
2 bytes saved thanks to @fergusq
{[[split()|reverse]&" "]}
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 */
-
\$\begingroup\$
splituses space as the default separator, sosplit()is shorter than(_/" ")(). \$\endgroup\$fergusq– fergusq2017年05月19日 16:45:41 +00:00Commented May 19, 2017 at 16:45
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.
Wren, 40 bytes
Fn.new{|a|a.split(" ")[-1..0].join(" ")}
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
Ohm, 4 bytes
z]Qù
Explanation
z Split the input on spaces.
] Dump it onto the stack.
Q Reverse the stack.
ù Join the stack with spaces. Implicit output.
CJam, 7 bytes
qS/W%S*
Explanation
q e# Read input
S/ e# Split on spaces
W% e# Reverse
S* e# Join with spaces
TAESGL, 7 bytes
ĴS)Ř)ĴS
Explanation
ĴS)Ř)ĴS
AĴS) implicit input "A" split at " "
Ř) reversed
ĴS joined with " "
J, 6 bytes
|.&.;:
Try it online! This is reverse (|.) under (&.) words (;:). That is, split sentence into words, reverse it, and join the sentence again.
['man', 'bites', 'dog']) \$\endgroup\$