Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Challenges

Reverse an ASCII string

+11
−0

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.
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

1 comment thread

General comments (7 comments)

33 answers

You are accessing this answer with a direct link, so it's being shown above all other answers regardless of its score. You can return to the normal view.

+6
−0

JavaScript, 25 24 bytes

-1 byte thanks to @Arnauld‭

f=([a,...b])=>a?f(b)+a:b

Try it online!

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

1 comment thread

General comments (2 comments)
+7
−0

Brain-Flak, 12 bytes

{({}<>)<>}<>

Try it online!

# (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
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+7
−0

Ahead, 3 bytes

SW@

S    Slurp entire input to stack
 W   Write entire stack
  @  End

Try it online!

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

1 comment thread

General comments (2 comments)
+6
−0

C (gcc), 34 bytes

f(char*s){*s&&f(s+1)^putchar(*s);}

Try it online!

Alternative:

C (gcc), 35 bytes

f(s){read(0,&s,1)&&f()^putchar(s);}

Try it online!

Alternative #2:

C (gcc), 36 bytes

f(s){write(read(0,&s,1)&&f(),&s,1);}

Try it online!

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+5
−0

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.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+5
−0

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];}

Try it online!

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

1 comment thread

General comments (4 comments)
+4
−0

Python 3, 20 bytes

print(input()[::-1])

Try it online!

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+4
−0

Japt, 1 byte

w

Uh... yeah. Probably similar solutions for many other golf langs.

Try it

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+4
−0

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.

Try it online!

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+4
−0

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);}

Try it online!

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

2 comment threads

Doesn't work on my system (4 comments)
General comments (1 comment)
+3
−0

Ruby, 14 bytes

->s{s.reverse}

Try it online!

a straightforward builtin.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+3
−0

Perl 5 -p, 10 bytes

$_=reverse

Try it online!

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+3
−0

Scala, 9 bytes

_.reverse

Try it in Scastie!

Explanation:

_.reverse
_          //The function argument
 .         //Call a method
  reverse  //Reverse the string
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+3
−0

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.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+3
−0

PowerShell, 43 bytes

param($s)(($s=$s|% t*y)|%{$s[--$q]})-join''

Try it online!

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+3
−0

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
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+2
−0

Bash, 3 bytes

rev

Try it online!

Less boring:

Bash, 8 bytes

tac -rs.

Try it online!

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+2
−0

Sclipting, (UTF-16) 2 bytes

Yay for builtins~

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+2
−0

Haskell, 7 bytes

reverse

Try it online!

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+2
−0

Befunge-98, 45 39 37 33 29 21 bytes

v:~<
>a-|
>v$<
,:
^_@

Try it online!

This answer probably sucks :P

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

1 comment thread

General comments (2 comments)

Sign up to answer this question »