58
\$\begingroup\$

Requirements:

  • Take an input on stdin including new lines / carriage returns of unlimited length (only bounded by system memory; that is, there is no inherent limit in the program.)
  • Output the reverse of the input on stdout.

Example:

Input:

Quick brown fox
He jumped over the lazy dog

Output:

god yzal eht revo depmuj eH
xof nworb kciuQ

Shortest wins.

Leaderboard:

var QUESTION_ID=242,OVERRIDE_USER=61563;function answersUrl(e){return"https://api.stackexchange.com/2.2/questions/"+QUESTION_ID+"/answers?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+ANSWER_FILTER}function commentUrl(e,s){return"https://api.stackexchange.com/2.2/answers/"+s.join(";")+"/comments?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+COMMENT_FILTER}function getAnswers(){jQuery.ajax({url:answersUrl(answer_page++),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){answers.push.apply(answers,e.items),answers_hash=[],answer_ids=[],e.items.forEach(function(e){e.comments=[];var s=+e.share_link.match(/\d+/);answer_ids.push(s),answers_hash[s]=e}),e.has_more||(more_answers=!1),comment_page=1,getComments()}})}function getComments(){jQuery.ajax({url:commentUrl(comment_page++,answer_ids),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){e.items.forEach(function(e){e.owner.user_id===OVERRIDE_USER&&answers_hash[e.post_id].comments.push(e)}),e.has_more?getComments():more_answers?getAnswers():process()}})}function getAuthorName(e){return e.owner.display_name}function process(){var e=[];answers.forEach(function(s){var r=s.body;s.comments.forEach(function(e){OVERRIDE_REG.test(e.body)&&(r="<h1>"+e.body.replace(OVERRIDE_REG,"")+"</h1>")});var a=r.match(SCORE_REG);a&&e.push({user:getAuthorName(s),size:+a[2],language:a[1],link:s.share_link})}),e.sort(function(e,s){var r=e.size,a=s.size;return r-a});var s={},r=1,a=null,n=1;e.forEach(function(e){e.size!=a&&(n=r),a=e.size,++r;var t=jQuery("#answer-template").html();t=t.replace("{{PLACE}}",n+".").replace("{{NAME}}",e.user).replace("{{LANGUAGE}}",e.language).replace("{{SIZE}}",e.size).replace("{{LINK}}",e.link),t=jQuery(t),jQuery("#answers").append(t);var o=e.language;/<a/.test(o)&&(o=jQuery(o).text()),s[o]=s[o]||{lang:e.language,user:e.user,size:e.size,link:e.link}});var t=[];for(var o in s)s.hasOwnProperty(o)&&t.push(s[o]);t.sort(function(e,s){return e.lang>s.lang?1:e.lang<s.lang?-1:0});for(var c=0;c<t.length;++c){var i=jQuery("#language-template").html(),o=t[c];i=i.replace("{{LANGUAGE}}",o.lang).replace("{{NAME}}",o.user).replace("{{SIZE}}",o.size).replace("{{LINK}}",o.link),i=jQuery(i),jQuery("#languages").append(i)}}var ANSWER_FILTER="!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe",COMMENT_FILTER="!)Q2B_A2kjfAiU78X(md6BoYk",answers=[],answers_hash,answer_ids,answer_page=1,more_answers=!0,comment_page;getAnswers();var SCORE_REG=/<h\d>\s*([^\n,]*[^\s,]),.*?(\d+)(?=[^\n\d<>]*(?:<(?:s>[^\n<>]*<\/s>|[^\n<>]+>)[^\n\d<>]*)*<\/h\d>)/,OVERRIDE_REG=/^Override\s*header:\s*/i;
body{text-align:left!important}#answer-list,#language-list{padding:10px;width:290px;float:left}table thead{font-weight:700}table td{padding:5px}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link rel="stylesheet" type="text/css" href="//cdn.sstatic.net/codegolf/all.css?v=83c949450c8b"> <div id="answer-list"> <h2>Leaderboard</h2> <table class="answer-list"> <thead> <tr><td></td><td>Author</td><td>Language</td><td>Size</td></tr></thead> <tbody id="answers"> </tbody> </table> </div><div id="language-list"> <h2>Winners by Language</h2> <table class="language-list"> <thead> <tr><td>Language</td><td>User</td><td>Score</td></tr></thead> <tbody id="languages"> </tbody> </table> </div><table style="display: none"> <tbody id="answer-template"> <tr><td>{{PLACE}}</td><td>{{NAME}}</td><td>{{LANGUAGE}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table> <table style="display: none"> <tbody id="language-template"> <tr><td>{{LANGUAGE}}</td><td>{{NAME}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table>

\$\endgroup\$
  • 5
    \$\begingroup\$ Do you allow standard library functions like PHP strrev \$\endgroup\$ – Ming-Tang Jan 31 '11 at 6:46
  • \$\begingroup\$ Is the output allowed to put the input's last newline at the beginning instead of the end? \$\endgroup\$ – Joey Adams Feb 2 '11 at 18:33
  • \$\begingroup\$ @Joey Adams, yep, it should replicate the input exactly. \$\endgroup\$ – Thomas O Feb 2 '11 at 21:20
  • 54
    \$\begingroup\$ Your example is somewhat wrong. The reverse of your input would be: ƃop ʎzɐʃ ǝɥʇ ɹǝʌo pǝdɯnɾ ǝH xoɟ uʍoɹq ʞɔınΌ ;-P \$\endgroup\$ – ninjalj Feb 4 '11 at 22:40
  • \$\begingroup\$ Need I only support characters which can be input into the system executing the code? \$\endgroup\$ – Golden Ratio Mar 3 '17 at 11:34

100 Answers 100

0
\$\begingroup\$

Add++, 32 bytes

x:1
y:''
Wx,]getchar,`y,x+,`x
oy

Try it online!

Fairly basic, although STDIN support was recently added to Add++, so I decided to show it off.

First, we set the two variables we need:

x:1
y:''

x to the integer 1 and y to the empty string. Next, we loop over each character in STDIN:

Wx,]getchar,`y,x+,`x

This is a while loop, with the condition simply being x. Each , denotes the separation of a new command, so the code is expanded into

Wx,
  ]getchar
  `y
  x+
  `x

]getchar is an additional, Add++ jargon for an extended command that is prefixed with a ]. Here, it simply reads a character from STDIN and assigns that to x. If the end of STDIN is reached, an empty string is returned.

Next, with

`y
x+
`x

We prepend this character to y, effectively building the string in reverse.

Once all input has been read, the x variable contains the empty string, and the while loop is terminated. Then we reach the final command

oy

This uses prefix notation: o is the command, and y indicates the variable to operate here. Here, o means output, without a trailing newline. y contains the input reversed, so this outputs our final result.

\$\endgroup\$
0
\$\begingroup\$

Pepe, 16 bytes

REEeREEEEeEeeReee

Try it online! (Compiler makes whitespaces when doing links between e and r)

Explanation:

REEeREEEEeEeeReee - full program

REEe              - insert input as string
    REEEEeEee     - reverse whole stack
             Reee - output
\$\endgroup\$
0
\$\begingroup\$

Ahead,  9  13 bytes

~ilj~#
 >dko@

This one will function properly when NULs are on the input.

Try it online!

\$\endgroup\$
0
\$\begingroup\$

PowerShell, 38 bytes

Thanks to Joey for the $($input) expression.

$($input)-join'
'|% t*y|%{$s=$_+$s}
$s

Try it online!


Alternative, 38 bytes

@($input)-join'
'|% t*y|%{$s=$_+$s}
$s

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Pip, 3 bytes

RVq

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Brain-Flak, 12 bytes

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

Not going to be winning with this, but it works

Try it Online!

\$\endgroup\$
0
\$\begingroup\$

Rust, 57 bytes

fn q(s:&str)->String{s.chars().rev().collect::<String>()}

Try it online!

Dart, 34 bytes

f(s)=>s.split('').reversed.join();

Try it online!

Pretty convoluted, you have to get a String List then reverse it and join it back for it to work.

\$\endgroup\$
0
\$\begingroup\$

Triangular, 14 bytes

(\~(#vp]<./)?<

Try it online!

I am almost 100% certain that ,~#n^`>p/ (9 bytes) would work if IP switches behaved in accordance with their specification, but in the meantime, 14 ain't too shabby.

Ungolfed:

     ( 
    \ ~ 
   ( # v 
  p ] < .
 / ) ? <
----------------------------------------------
(                Set a point for the IP to jump to
 ~v<             Read a character from input, change directions twice
    ?)/          ) returns to the previously set point. ? will skip the jump back if ToS < 0
       p(        Pop the top value of the stack (the null-input), then set a new jump point
         /#<[    Pop the top value of the stack and print that value, then jump back if ToS > 0
\$\endgroup\$
0
\$\begingroup\$

Elixir, 42 bytes

IO.puts String.reverse IO.read :stdio,:all
\$\endgroup\$
0
\$\begingroup\$

Poetic, 60 bytes

normally i create a reversal
so could i?i suppose maybe i do

Try it online!

This is actually slightly modified from an example on my website. The only things changed are that the letters are now stored right to left instead of left to right, the 11 letter word is replaced with two 1-letter words, and there is no 0 command at the end (which results in an error, but that's okay).

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.