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 …
3y ago
[Ahead], 3 bytes SW@ …
3y ago
[C (gcc)], 34 bytes …
3y ago
JavaScript, 25 24 bytes -1 …
3y ago
jq, 16 bytes, preferably `-rR` …
2y ago
C, 66 59 bytes -7 bytes tha …
3y ago
[Python 3], 20 bytes …
3y ago
[Shakespeare Programming Langu …
3y ago
C (gcc), 62 bytes main( …
2y ago
Japt, 1 byte w Uh... …
3y ago
Turing Machine.io, 363 bytes …
3y ago
[PowerShell], 43 bytes …
3y ago
Scala, 9 bytes ```scala .rev …
3y ago
[Perl 5] `-p`, 10 bytes …
2y ago
[Ruby], 14 bytes -> …
3y ago
Laser, 9 bytes ``` c⌜ps …
3y ago
[Haskell], 7 bytes …
2y ago
[Sclipting], (UTF-16) 2 bytes …
2y ago
[Lua], 32 bytes pri …
2y ago
[Bash], 3 bytes rev …
3y ago
Befunge-98, 45 39 37 33 29 21 …
3y ago
Python, 16 bytes ```python …
9mo ago
J, 2 bytes ```J |. ``` …
2y ago
[shortC], 24 bytes f(Cs …
2y ago
Rockstar, 67 bytes list …
2y ago
[PHP], 32 24 bytes …
2y ago
Standard ML, 23 bytes i …
9mo ago
R, 32 bytes ```r \(s)intTo …
9mo ago
R + magrittr, 31 bytes …
9mo ago
Brainfuck, 10 Bytes ```,[ …
9mo ago
SOS, 76 bytes +>+>????? …
2y ago
[Java (JDK)], 88 83 bytes …
2y ago
32 answers
Brainfuck, 10 Bytes
,[>,]<[.<]
Requires EOF to be 0.
,[>,]<[.<]
, # Read character
[ ] # loop as long as current cell is not 0 (EOF)
>, # read and fill the tape to the right with the read characters
< # go back to the last valid character, i.e. one before EOF
[ ] # loop till current cell is 0 (i.e. cell pointer is before the first read character
.< # output character and move left
0 comment threads
Java (JDK), 88 83 bytes
static void f(String s){for(int i=s.length();i>0;System.out.print(s.charAt(--i)));}
0 comment threads
shortC, 24 bytes
f(C*s){*s&&f(s+1)^P*s);}
A shortC conversion of @Sisyphus's C (gcc) implementation.
0 comment threads
Rockstar, 67 bytes
listen to S
split S
O's""
while S
roll S into C
let O be C+O
say O
Try it here (Code will need to be pasted in)
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
0 comment threads
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.
1 comment thread