Reverse an ASCII string
Your goal is to reverse an ascii string. Given a (optionally newline or null terminated) input, output your input in reverse order, optionally followed by a newline. Terminate afterward. Function answers will not be given a newline, and are not expected to output one unless they print the answer to console.
Examples
Assume all inputs are followed by a newline, and are all standard ASCII encoded.
abcdef
-> fedcba
Hello, World!
-> !dlroW ,olleH
racecar
-> racecar
Example program
function solution(x) {
return x.split("").reverse().join("");
}
Further clarifications
- No, you don't have to handle nulls correctly.
- Nor empty inputs.
[Ahead], 3 bytes [email protected] …
~2mo ago
C, 66 59 bytes -7 bytes tha …
~1mo ago
[JavaScript (Node.js)], 25 byt …
~2mo ago
Japt, 1 byte w Uh... …
~2mo ago
[Shakespeare Programming Langu …
~1mo ago
[C (gcc)], 34 bytes …
~1mo ago
[Brain-Flak], 12 bytes …
13d ago
[Bash], 3 bytes rev …
~1mo ago
Perl 5 `-p`, 10 bytes `$=re …
~1mo ago
[Ruby], 14 bytes -> …
~2mo ago
Laser, 9 bytes ``` c⌜ps …
~1mo ago
C (gcc), 62 bytes main( …
~2mo ago
Befunge-98, 45 39 37 33 29 21 …
~2mo ago
13 answers
Ahead, 3 bytes
[email protected]
S Slurp entire input to stack
W Write entire stack
@ End
2 comments
Why does this reverse the input string?
Input is pushed to the stack one character at a time. When it's printed the last character is the top of the stack, so it's popped and printed first.
C, 66 59 bytes
-7 bytes thanks to Lundin!
In-place string reversal
f(char*s){s[1]?f(s+1):0;for(char t=*s;s[1];*++s=t)*s=s[1];}
4 comments
I think this might be a possible improvement: f(char*s){s[1]?f(s+1):0;while(s[1]){char t=*s;*s=s[1];*++s=t;}}}
. In case s[1] is a character, it makes the recursive call and then while loop. If not, a 0;
dummy statement. s[1]
is zero in that case so the while loop isn't executed.
Also, switching while
with for
will save you lots. for(char t=*s;s[1];*++s=t;)*s=s[1];
should work. So how about this? f(char*s){s[1]?f(s+1):0;for(char t=*s;s[1];*++s=t)*s=s[1];}
, 59 bytes.
@Lundin Thanks for your suggestions!
Oh, and t
actually doesn't have to be char. So with gcc implicit int abuse, this should be possible: t;f(char*s){s[1]?f(s+1):0;for(t=*s;s[1];*++s=t)*s=s[1];}
.
Shakespeare Programming Language, 184 bytes
,.Ajax,.Puck,.Act I:.Scene I:.[Enter Ajax and Puck]Scene V:.Puck:Open mind!Be you worse zero?If soLet usScene L!Remember you!Let usScene V!Scene L:.Puck:Recall!Speak thy!Let usScene L!
Surprisingly short for SPL. Errors out with a runtime.
Adds input to the stack, then recalls it. Since SPL handles one character at a time, this automatically reverses the input. SPL also has a built-in function, where if there is no input, it will return -1
, which allows us to see the end of input.
0 comments
Laser, 9 bytes
c⌜ps
\U#
My own language's showcase time! This is a 2D language with an instruction pointer initially pointing to the right. It takes implicit input as an array of characters.
Explanation:
c⌜p repeat as long as the current stack (input) isn't empty:
s pop from the current stack and push onto the next stack (the instruction pointer then loops back around to the c)
⌜
\U# once the current stack is empty, switch direction, get bounced to the right on the second line, move up a stack, and output it
0 comments
Brain-Flak, 12 bytes
{({}<>)<>}<>
# (Implicitly) Read characters from STDIN and place them all on the active stack
# While the top of the active stack is not null...
{
# Push the following value on to the active stack
(
# Pop the current character off the active stack and add it to the value we're tracking
{}
# Toggle which stack is active
<>
# (push)
)
# Toggle back to the stack the input was on
<>
# (endwhile)
}
# Toggle stacks back so that the secondary stack is active when the program ends
<>
# (Implicitly) Print all values on the active stack
0 comments
Befunge-98, 45 39 37 33 29 21 bytes
v:~<
>a-|
>v$<
,:
^[email protected]
This answer probably sucks :P
2 comments
16 bytes with no newline output, >5+#:-#~_$>:#,[email protected]
Make your own post, it's a more intelligent solution than mine :P
C (gcc), 62 bytes
main(){char b[99],*p=strchr(gets(b),0);for(;p-->b;)putch(*p);}
This relies on the usual gcc extension abuse. It assumes that max user input is 98 characters + null term, since this wasn't specified.
1 comment
*p=b+strlen(gets(b))
works too but gives identical size.
7 comments
I would recommend allowing programs to reverse all of STDIN as well. Also, forcing an extra newline to be output rather than allowing it to be optional feels like it makes the challenge more complex than it needs to be. Also, do we have to handle empty inputs? — Jo King about 2 months ago
@Jo King Codidact has a "suggest changes" feature right? Can you please use that? As for handling empty inputs: No. — moony about 2 months ago
There's some artifacts from this challenge being written around an answer instead of the other way around. — moony about 2 months ago
@moony Suggest edits aren't really used for stuff like that; since there are a bunch of limitations (you can't suggest more than one edit, if you change your mind you can't edit your suggested edit, you can't partially incorporate edits etc.) It's much better to use the comments for these sorts of suggestions. — Moshi about 2 months ago
(Aside: to mention someone you have to put the full username, without spaces) — Moshi about 2 months ago