Take the 2-minute tour ×
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.

Referring to the printable ASCII character codes in decimal, we know that from 32 to 126 we have the printable characters with 32 being (space). Your challenge is to write a program using only characters from 32 to 125 (excluding 126) which when executed, prints its own source code except that each character in the source code has its ASCII code increased by one.

For example, if the source code was

main(){printf("Hello World");}

its output would be

nbjo)*|qsjoug)#Ifmmp!Xpsme#*<~

The following are forbidden:

  • Reading/writing/using any external files or storage (including the internet)
  • Reading/echoing your own source code
  • Source codes with fewer than 2 characters (they are no fun). Must be greater than or equal to 2.

Making it a popularity contest where after waiting for at least two weeks, the answer, in any language, with the highest upvotes wins with the smaller character count being the tie-breaker.

share|improve this question
    
Why not allow 126, but it has to go to 32? (I don't know why/how anyone would do this.) –  Quincunx Jan 5 at 4:02
    
These kinds of questions should also forbid error messages to be considered as "output" (sorry @GariBN). "Output" is stuff deliberately printed on standard output by your code, not arbitrary side effects determined by the language executable. –  l0b0 Jan 5 at 17:44
2  
The inability to use ~ really sucks for GolfScript. Bet you did that on purpose. :-) –  Ilmari Karonen Jan 5 at 17:51
    
define "reading your own source code". Are we allowed to stringify a function, or we have to eval a string instead? –  Jan Dvorak Feb 24 at 5:52
add comment

15 Answers

up vote 22 down vote accepted

Python (27 characters)

In the Python shell, the following script will output the desired result:

TzoubyFssps;!jowbmje!tzouby

Yes! it outputs:

SyntaxError: invalid syntax
share|improve this answer
1  
Oh man, didn't see this coming. :-D –  Fixed Point Jan 5 at 9:05
4  
Actually the result is shifted the wrong way :P –  Erbureth Jan 5 at 18:37
    
@Erbureth This is relevant also for many other answers here... =) –  Gari BN Jan 6 at 15:28
    
Smart move, although it's not the shortest. xD –  Trimsty Jan 7 at 12:47
    
I am calling Trollololololol. –  PyRulez Feb 25 at 14:02
add comment

huh?, 5 characters

Ntbg 

Note that the 5th character is a space after Ntbg. This is the same trick as in one of my previous answers. Ntbg is an invalid path, so the interpreter will output Ouch! You should run it like this:

huh.exe Ntbg 
share|improve this answer
    
+1 ... I love it! –  Tim Seguine Jan 5 at 20:53
add comment

PHP (351)

I'm sure there's a better way to do this, as I am new to codegolfing, but here's my PHP solution:

function q(){$q=chr(39);$a=array('function q(){$q=chr(39);$a=array(',');@$r.=$a[0];foreach($a as$b)$r.=$q.$b.$q.",";$r=substr($r,0,-1);$r.=$a[1];for($i=0;$i<strlen($r);$i++)$r{$i}=chr(ord($r{$i})+1);return $r;}');@$r.=$a[0];foreach($a as$b)$r.=$q.$b.$q.",";$r=substr($r,0,-1);$r.=$a[1];for($i=0;$i<strlen($r);$i++)$r{$i}=chr(ord($r{$i})+1);return $r;}

Output:

gvodujpo!r)*|%r>dis)4:*<%b>bssbz)(gvodujpo!r)*|%r>dis)4:*<%b>bssbz)(-(*<A%s/>%b\1^<gpsfbdi)%b!bt%c*%s/>%r/%c/%r/#-#<%s>tvctus)%s-1-.2*<%s/>%b\2^<gps)%j>1<%j=tusmfo)%s*<%j,,*%s|%j~>dis)pse)%s|%j~*,2*<sfuvso!%s<~(*<A%s/>%b\1^<gpsfbdi)%b!bt%c*%s/>%r/%c/%r/#-#<%s>tvctus)%s-1-.2*<%s/>%b\2^<gps)%j>1<%j=tusmfo)%s*<%j,,*%s|%j~>dis)pse)%s|%j~*,2*<sfuvso!%s<~
share|improve this answer
add comment

GolfScript, 19 chars

".`\\+{)}%".`\+{)}%

A fairly straightforward solution based on my entry to the "reverse quine" challenge. It starts with the double-qouted string ".`\\+{)}%"; . duplicates it, ` un-evals the second copy to restore the quotes (and double the backslash), \ swaps the copies, + concatenates them and {)}% increments each character by one.

(If the ASCII character 126 (tilde, ~) wasn't disallowed, we could do this in in 14 characters with {`".~"+{)}%}.~. Since it is, however, that particular quining trick doesn't work.)

Ps. Yes, I know this is a rather than strict , but else am I going to do with GolfScript — ASCII art? ;-)

share|improve this answer
add comment

TI-BASIC, 10

For your TI-83/84 calculator!

DQQ9RXMS@W

Outputs:

ERR:SYNTAX
share|improve this answer
add comment

JavaScript, 117 characters

I know it's not code golf but I golfed it anyway.

function f(){alert((f+';f()').split('').map(function(x){return String.fromCharCode(x.charCodeAt()+1)}).join(''))};f()

(I'm not reading my own source code; I'm simply using the Function object's toString function.)

share|improve this answer
add comment

Java - 1331 bytes, 618 bytes and 504 bytes

Here it is in java. The cool thing is that it is pretty legible and flexible. You may experiment to change the SHIFT variable to 0 and it will be a quine. You may change it to whatever value you want, including negative values and it will shift the code accordingly.

public class Quinex {

    private static final int SHIFT = 1;

    private static String next(String now, boolean mangles) {
        String sb = "";
        for (char c : now.toCharArray()) {
            if (!mangles && c == 87) {
                sb += next(String.valueOf(SHIFT), true);
            } else {
                sb += (char) ((mangles ? c : c == 94 ? 10 : c == 64 ? 34 : c) + SHIFT);
            }
        }
        return sb;
    }

    public static void main(String... args) {
        System.out.println(next(TEXT, false) + next(TEXT, true) + new String(new char[] { 34, 59, 10, 125 }));
    }

    private static final String TEXT = "public class Quinex {^^    private static final int SHIFT = W;^^    private static String next(String now, boolean mangles) {^        String sb = @@;^        for (char c : now.toCharArray()) {^            if (!mangles && c == 87) {^                sb += next(String.valueOf(SHIFT), true);^            } else {^                sb += (char) ((mangles ? c : c == 94 ? 10 : c == 64 ? 34 : c) + SHIFT);^            }^        }^        return sb;^    }^^    public static void main(String... args) {^        System.out.println(next(TEXT, false) + next(TEXT, true) + new String(new char[] { 34, 59, 10, 125 }));^    }^^    private static final String TEXT = @";
}

However, the only drawback in the previous class are the line breaks, which are not permited in the question spec (are outside the range 32 to 125). So I give here a golfed version that is free of line breaks (and free of the quirks to handle them). You may edit the value of the S variable to change the shift. This has 618 bytes:

class Q{static String g(String p,int m){int S=1;String u="";for(char c:p.toCharArray()){if(m==0&&c==87){u+=g(String.valueOf(S),1);}else{u+=(char)((m>0?c:c==64?34:c)+S);}}return u;}public static void main(String[]y){System.out.println(g(T,0)+g(T,1)+new String(new char[]{34,59,125}));}static final String T="class Q{static String g(String p,int m){int S=W;String u=@@;for(char c:p.toCharArray()){if(m==0&&c==87){u+=g(String.valueOf(S),1);}else{u+=(char)((m>0?c:c==64?34:c)+S);}}return u;}public static void main(String[]y){System.out.println(g(T,0)+g(T,1)+new String(new char[]{34,59,125}));}static final String T=@";}

Surely, if we drop the fine adjustment of the offset and hardcode the value of the shift, we can do a completely golfed version with 504 bytes:

class Q{static String g(String p,int m){String u="";for(char c:p.toCharArray()){u+=(char)((m>0?c:c==64?34:c)+1);}return u;}public static void main(String[]y){System.out.println(g(T,0)+g(T,1)+new String(new char[]{34,59,125}));}static final String T="class Q{static String g(String p,int m){String u=@@;for(char c:p.toCharArray()){u+=(char)((m>0?c:c==64?34:c)+1);}return u;}public static void main(String[]y){System.out.println(g(T,0)+g(T,1)+new String(new char[]{34,59,125}));}static final String T=@";}
share|improve this answer
add comment

Perl 5, 284 characters include the linefeeds

Not being allowed to use ~ made it a bit more tricky.

#!/usr/bin/perl
$_=<<X;
#!/usr/bin/perl
Y
\$a=\$_;
s/\\\\/\\\\\\\\/g;
s/\\\$/\\\\\\\$/g;
\$b=\$_;
\$_=\$a;
s/Y/\\\$\\_\\=\\<\\<X\\;\\n\${b}X/;
s/(.)/chr(ord(\$1)+1)/ge;
print;
X
$a=$_;
s/\\/\\\\/g;
s/\$/\\\$/g;
$b=$_;
$_=$a;
s/Y/\$\_\=\<\<X\;\n${b}X/;
s/(.)/chr(ord($1)+1)/ge;
print;
share|improve this answer
add comment

Lua - 192

Pretty straightforward,

s=[[print((('s=['..'['..s..']'..']'..s):gsub('.',function(x)return string.char(x:byte()+1)end)))]]print((('s=['..'['..s..']'..'];'..s):gsub('.',function(x)return string.char(x:byte()+1)end)))
share|improve this answer
add comment

C - 156

char*f="char*f=%c%s%c,q[200];i;main(){sprintf(q,f,34,f,34);while(q[i])q[i++]++;puts(q);}",q[200];i;main(){sprintf(q,f,34,f,34);while(q[i])q[i++]++;puts(q);}

Just the classic C quine with required modifications

P.S, apparently sprintf(f,...,f,...) is a segfault.

share|improve this answer
add comment

JavaScript (276)

Without using .toString():

function q(){x=String.fromCharCode;y=x(39);a=['function q(){x=String.fromCharCode;y=x(39);a=[','];r=a[0]+y+a[0]+y+","+y+a[1]+y+a[1];q="";for(c in r)q+=x(r[c].charCodeAt(0)+1);return q;}'];r=a[0]+y+a[0]+y+","+y+a[1]+y+a[1];q="";for(c in r)q+=x(r[c].charCodeAt(0)+1);return q;}
share|improve this answer
add comment

Python, 99

s='import sys;sys.stdout.write("".join(chr(ord(c)+1)for c in a%(s,a)))';a='s=%r;a=%r;exec s';exec s

Output:

t>(jnqpsu!tzt<tzt/tuepvu/xsjuf)##/kpjo)dis)pse)d*,2*gps!d!jo!b&)t-b***(<b>(t>&s<b>&s<fyfd!t(<fyfd!t

This can be shortened to 75 chars, but it will print a new line character after the output, technically breaking the rules:

s='print"".join(chr(ord(c)+1)for c in a%(s,a))';a='s=%r;a=%r;exec s';exec s
share|improve this answer
add comment

Ruby, 63

Darn, can't use a heredoc on this one.

s="s=%p;(s%%s).chars{|c|$><<c.succ}";(s%s).chars{|c|$><<c.succ}

Ruby has a native method .succ that does this on a character, and printing without a newline is shorter than printing with, so this works out pretty neatly.

share|improve this answer
add comment

C, 153

main(a){char b[999];sprintf(b,a="main(a){char b[999];printf(a=%c%s%c,34,a,34);for(a=0;b[a];)b[a++]++;puts(b);}",34,a,34);for(a=0;b[a];)b[a++]++;puts(b);}

Another modification of the classic quine in c...

share|improve this answer
add comment

Python

  • Create an empty file,
  • name it python_empty_file.py,
  • execute it with python python_empty_file.py,
  • notice there is no output. So it is not in disagreement with the request.
share|improve this answer
    
-1 Source code must be at least two characters. –  Fixed Point Feb 24 at 16:50
add comment

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.