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

Dashboard
Notifications
Mark all as read
Challenges

Reverse an ASCII string

+6
−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.
Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

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

Show 2 more comments

13 answers

+4
−0

Ahead, 3 bytes

[email protected]

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

Try it online!

Why does this post require moderator attention?
You might want to add some details to your flag.

2 comments

Why does this reverse the input string? dessert‭ about 1 month ago

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. snail_‭ about 1 month ago

+3
−0

Japt, 1 byte

w

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

Try it

Why does this post require moderator attention?
You might want to add some details to your flag.

0 comments

+3
−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!

Why does this post require moderator attention?
You might want to add some details to your flag.

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. Lundin‭ about 1 month ago

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‭ about 1 month ago

@Lundin Thanks for your suggestions! Moshi‭ about 1 month ago

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];}. Lundin‭ about 1 month ago

+3
−0

JavaScript (Node.js), 25 bytes

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

Try it online!

Why does this post require moderator attention?
You might want to add some details to your flag.

0 comments

+2
−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!

Why does this post require moderator attention?
You might want to add some details to your flag.

0 comments

+2
−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!

Why does this post require moderator attention?
You might want to add some details to your flag.

0 comments

+1
−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
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comments

+1
−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
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comments

+1
−0

Ruby, 14 bytes

->s{s.reverse}

Try it online!

a straightforward builtin.

Why does this post require moderator attention?
You might want to add some details to your flag.

0 comments

+1
−0

Bash, 3 bytes

rev

Try it online!

Less boring:

Bash, 8 bytes

tac -rs.

Try it online!

Why does this post require moderator attention?
You might want to add some details to your flag.

0 comments

+1
−0

Perl 5 -p, 10 bytes

$_=reverse

Why does this post require moderator attention?
You might want to add some details to your flag.

0 comments

+0
−0

Befunge-98, 45 39 37 33 29 21 bytes

v:~<
>a-|
>v$<
,:
^[email protected]

Try it online!

This answer probably sucks :P

Why does this post require moderator attention?
You might want to add some details to your flag.

2 comments

20 bytes Jo King‭ about 2 months ago

16 bytes with no newline output, >5+#:-#~_$>:#,[email protected] Make your own post, it's a more intelligent solution than mine :P moony‭ about 2 months ago

+0
−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.

Why does this post require moderator attention?
You might want to add some details to your flag.

1 comment

*p=b+strlen(gets(b)) works too but gives identical size. Lundin‭ about 2 months ago

Sign up to answer this question »

This community is part of the Codidact network. We have other communities too — take a look!

You can also join us in chat!

Want to advertise this community? Use our templates!