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.
[Brain-Flak], 12 bytes …
4y ago
[Ahead], 3 bytes SW@ …
4y ago
[C (gcc)], 34 bytes …
4y ago
JavaScript, 25 24 bytes -1 …
3y ago
jq, 16 bytes, preferably `-rR` …
3y ago
C, 66 59 bytes -7 bytes tha …
4y ago
[Python 3], 20 bytes …
3y ago
[Shakespeare Programming Langu …
4y ago
C (gcc), 62 bytes main( …
3y ago
Japt, 1 byte w Uh... …
4y ago
Turing Machine.io, 363 bytes …
3y ago
[PowerShell], 43 bytes …
3y ago
Scala, 9 bytes ```scala .rev …
4y ago
[Perl 5] `-p`, 10 bytes …
3y ago
[Ruby], 14 bytes -> …
4y ago
Laser, 9 bytes ``` c⌜ps …
4y ago
[Haskell], 7 bytes …
3y ago
[Sclipting], (UTF-16) 2 bytes …
3y ago
[Lua], 32 bytes pri …
3y ago
[Bash], 3 bytes rev …
4y ago
Befunge-98, 45 39 37 33 29 21 …
4y ago
Python, 16 bytes ```python …
1y ago
J, 2 bytes ```J |. ``` …
3y ago
[shortC], 24 bytes f(Cs …
3y ago
Rockstar, 67 bytes list …
3y ago
[PHP], 32 24 bytes …
3y ago
[AWK], 48 bytes ORS=""; …
7mo ago
Standard ML, 23 bytes i …
1y ago
R, 32 bytes ```r \(s)intTo …
1y ago
R + magrittr, 31 bytes …
1y ago
Brainfuck, 10 Bytes ```,[ …
1y ago
SOS, 76 bytes +>+>????? …
2y ago
[Java (JDK)], 88 83 bytes …
3y ago
33 answers
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 comment threads
JavaScript, 25 24 bytes
-1 byte thanks to @Arnauld
f=([a,...b])=>a?f(b)+a:b
jq, 16 bytes, preferably -rR
./""|reverse|add
Annoyingly, jq has convenient things like implicit I/O, and annoying
things like adding strings, but you can't reverse
a string >:|.
./""
&add
are slightly shorter than the more obvious
split("")
&join("")
. The division operator splits a string, and add
adds
all the elements of an array together, using string concatenation in this
case.
0 comment threads
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];}
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.
EDIT: Revisited, function-based equivalent of the above (plus I have no idea what compiler I used to get putch working, not gcc/Linux at least):
C (gcc), 53 bytes
*o;f(char*s){for(s=strchr(o=s,0);s-->o;)putchar(*s);}
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 comment threads
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 comment threads
Turing Machine.io, 363 bytes
0 0 0 0 -1 8 0
0 8 4 0 1 9 0
0 9 -1 0 0 1 0
0 1 0 0 1 9 0
1 1 1 0 1 9 0
2 1 2 0 1 9 0
3 1 0 0 -1 2 0
2 2 2 0 -1 3 0
0 3 0 0 -1 3 0
1 3 1 0 -1 3 0
2 3 2 0 1 4 0
0 4 0 0 -1 4 0
1 4 1 0 -1 4 0
2 4 2 0 1 5 0
0 5 0 1 1 5 0
1 5 1 1 1 5 0
2 5 2 2 -1 6 0
0 6 0 0 -1 6 0
1 6 0 0 -1 6 0
2 6 2 0 -1 7 0
4 6 4 0 0 7 1
0 7 0 0 -1 7 0
1 7 1 0 -1 7 0
2 7 2 0 1 5 0
4 7 4 0 1 5 0
This is probably very golf-able. I'll golf this down and add an explanation later.
0 comment threads
Scala, 9 bytes
_.reverse
Explanation:
_.reverse
_ //The function argument
. //Call a method
reverse //Reverse the string
1 comment thread