Tell me more ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

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.

share|improve this question
1  
Do you allow standard library functions like PHP strrev – SHiNKiROU Jan 31 '11 at 6:46
Is the output allowed to put the input's last newline at the beginning instead of the end? – Joey Adams Feb 2 '11 at 18:33
@Joey Adams, yep, it should replicate the input exactly. – Thomas O Feb 2 '11 at 21:20
4  
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 – ninjalj Feb 4 '11 at 22:40

20 Answers

up vote 10 down vote accepted

Golfscript - 3 chars

-1%

obfuscated version is also 3 chars

0(%

here is an explanation of how % works

share|improve this answer
2  
How can we ever compete with Golfscript?? – Thomas O Jan 30 '11 at 13:16
7  
@Thomas: By using FlogScript, I guess. In any case, if you post a trivial task, then expect solutions to be equally trivial. And if it takes three method calls in Python, if can just as well be three characters in Golfscript. – Joey Jan 30 '11 at 13:24
@Joey, you missed my humour. – Thomas O Jan 30 '11 at 17:00
1  
@Thomas: Sorry, it wasn't that obvious. Given that some members already had quiet heated discussions about this very language that seemingly was no humor, it wasn't too unreasonably to assume similar here. – Joey Jan 30 '11 at 18:16
3  
@Joey It was more a humourous despair as GolfScript seems like noise to the untrained eye. – Thomas O Jan 30 '11 at 18:31

Bash - 7

tac|rev

tac reverses line order, while rev reverses character order.

share|improve this answer
Let's just go the next step and alias that to a single letter bash command! alias z='tac|rev' – Daniel Standage Jan 31 '11 at 16:14
3  
@Diniel That's kinda the same as using compiler flags to define macros, i.e. against the spirit of code golf. – marcog Jan 31 '11 at 16:50

Haskell - 21

main=interact reverse
share|improve this answer
Not only short, but completely idiomatic as well :) – hammar Jun 11 '11 at 21:27

Here's my entry, Python, 41 40 chars:

import sys;print sys.stdin.read()[::-1]

41 -> 40 - removed semicolon at end of program.

Probably could be optimised!

share|improve this answer
I wish I had an easy way of reversing something in PowerShell ;-) – Joey Jan 30 '11 at 12:28
2  
Martian people, always useful. [::-1] – wok Feb 4 '11 at 16:16

C-37 bytes

main(_){write(read(0,&_,1)&&main());}
share|improve this answer
Cool, but it doesn't work for me. – Joey Adams Mar 18 '11 at 4:33
@Joey Adams:Try it out here. – Quixotic Mar 19 '11 at 1:06
Ah, I had to compile without optimization. – Joey Adams Apr 27 '11 at 16:59

Perl - 23

print scalar reverse <>
share|improve this answer
2  
You can remove the third space. – Timwi Mar 9 '11 at 1:37
In fact, print"".reverse<> is only 17 chars. And with Perl 5.10+ you can save two more chars by using say instead of print. – Ilmari Karonen Apr 29 '12 at 15:05

BrainFuck, 10 characters

,[>,]<[.<]

Beats a good amount of answers for such a simple language.

share|improve this answer
DNA reverses its order all the time, so maybe there's something fundamental about the nature of information and computation in what you have observed. I came across this solution while solving problems on rosalind.info with shell one-liners. – ixtmixilix Dec 18 '12 at 9:35

Windows PowerShell, 53 54

-join($x=[char[]]($($input)-join'
'))[($x.count)..0]

2011-01-30 (54) – First attempt

2011-01-30 (53) – Inline line breaks are fun.

2011-01-3- (52) – Inlined variable assignments too.

share|improve this answer

Ruby - 19 characters

puts$<.read.reverse
share|improve this answer

Befunge-93 - 11x2 (22 characters)

>~:0`v >:v
^    _$^,_@

Tested using this interperter.

share|improve this answer
4  
Are you sure you didn't just press random keys on your keyboard? – Thomas O Feb 2 '11 at 21:57
@Thomas - Are you sure you didn't try using the linked interpreter? It's web-based, in case you were worried about downloading anything. – MiffTheFox Feb 2 '11 at 22:08
1  
I'm only joking. I'm sure it will work, but it just looks like you pressed some keys randomly. That indicates a very compact language. – Thomas O Feb 2 '11 at 22:11

C - 47 characters

main(c){if(c=getchar(),c>=0)main(),putchar(c);}

Note that this uses O(n) stack space.

share|improve this answer
Simply Awesome! – st0le Feb 4 '11 at 16:57
Just your idea but this saves 2-3 key strokes:main(c){(c=getchar())>0&&main(),putchar(c);} – Quixotic Mar 9 '11 at 5:51

Python - 35 chars

import os;print os.read(0,2e9)[::-1]
share|improve this answer
This is limited to 1e9 characters, though... not technically unlimited. – Thomas O Jan 30 '11 at 13:15
2  
It's not even legal Python, it lacks an argument to os.read(). Should be os.read(0,...) – hallvabo Jan 30 '11 at 15:55
@hallvabo, oops you are correct, should be 35 chars.9e9 doesn't work because it is too big to be converted to an int, so 2e9 is as high as it can go. – gnibbler Jan 30 '11 at 20:44
what about sys.maxint? – Thomas O Jan 30 '11 at 20:46
1  
@gnibbler: import os,sys is possible. Even os.sys.maxint works for me (might be version/system dependent). – hallvabo Jan 30 '11 at 21:16
show 2 more comments

C++ - 168 chars

#include<algorithm>
#include<iostream>
#include<string>
using namespace std;main(){string m;for(string l;getline(cin,l);)m+=l+"\n";reverse(m.begin(),m.end());cout<<m;}
share|improve this answer

Perl

print scalar reverse for reverse(<STDIN>);
share|improve this answer

PHP - 38 17 characters

<?=strrev(`cat`);
share|improve this answer

PHP, 82 29 24 29 28 characters

<?=strrev(fread(STDIN,2e9));

82 -> 29: The new line character is preserved when reversed with strrev.
29 -> 24: Uses the shortcut syntax now
24 -> 29: Now reads all lines instead of a single line

share|improve this answer
One problem: fgets(STDIN) only reads the first line. – PleaseStand Feb 1 '11 at 0:09
Updated the code to now read all of the lines. – Kevin Brown Feb 5 '11 at 1:52
Except you have an artificial limit of 1000 chars – Charlie Somerville Feb 5 '11 at 9:55
Updated the limit to match the Python one below, I can't imagine anyone using that much though. – Kevin Brown Feb 6 '11 at 16:09

Groovy 44 characters

args.reverse().each{print it.reverse()+" "}

share|improve this answer

PHP - 44 characters

<?=strrev(file_get_contents('php://stdin'));
share|improve this answer

C# - 116

using System.Linq;using c=System.Console;class p{static void Main(){c.Write(c.In.ReadToEnd().Reverse().ToArray());}}
share|improve this answer

VB.Net - 96

Module M
Sub Main()
Console.Write(CStr(ConsoleIn.ReadToEnd.Reverse.ToArray))
End Sub
End Module

90 (With Turned off Option Strict)

Module M
Sub Main()
Console.Write(Console.In.ReadToEnd.Reverse.ToArray)
End Sub
End Module
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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