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

Comments on Reverse an ASCII string

Parent

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)
Post
+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)
Doesn't work on my system
H_H‭ wrote about 1 year ago

I tested it under Linux AMD64 with GCC 10.2.1. putch isn't know and the return value of gets() get converted to a int and back to a pointer, causing it to lose the upper halve of the pointer value causing a segment fault in strchr().

Lundin‭ wrote about 1 year ago

I think it might depend on which standard lib that is used (libc/glibc and so on). The revised 53 byte version should be pure standard C however.

H_H‭ wrote about 1 year ago · edited about 1 year ago

Maybe you can specify on which system you tested it? It is at least not standard C, since there is no putch() in standard C. And it probably needs a system where sizeof(int)==sizeof(char*), so that you don't lose anything after gets() return.

Lundin‭ wrote about 1 year ago

Well as it says in the (several years old) answer, I had no idea which system I got it running on when I did the edit. Might have been gcc/mingw/Windows. Anyway, probably best to refer to the function-only version.