15
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ Why not allow 126, but it has to go to 32? (I don't know why/how anyone would do this.) \$\endgroup\$ – Justin Jan 5 '14 at 4:02
  • \$\begingroup\$ 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. \$\endgroup\$ – l0b0 Jan 5 '14 at 17:44
  • 3
    \$\begingroup\$ The inability to use ~ really sucks for GolfScript. Bet you did that on purpose. :-) \$\endgroup\$ – Ilmari Karonen Jan 5 '14 at 17:51
  • 1
    \$\begingroup\$ define "reading your own source code". Are we allowed to stringify a function, or we have to eval a string instead? \$\endgroup\$ – John Dvorak Feb 24 '14 at 5:52
  • \$\begingroup\$ This problem seems not that suitable as a popularity contest. \$\endgroup\$ – l4m2 Apr 3 '18 at 5:01

20 Answers 20

46
\$\begingroup\$

Python (27 characters)

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

TzoubyFssps;!jowbmje!tzouby

Yes! it outputs:

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

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 
\$\endgroup\$
  • \$\begingroup\$ I can imagine the many quines with "Huh?" now. Reversed Quine: !hcuO Quine: Ouch! \$\endgroup\$ – MilkyWay90 Feb 5 at 23:07
8
\$\begingroup\$

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<~
\$\endgroup\$
6
\$\begingroup\$

TI-BASIC, 10

For your TI-83/84 calculator!

DQQ9RXMS@W

Outputs:

ERR:SYNTAX
\$\endgroup\$
6
\$\begingroup\$

GolfScript, 15 chars

{`{)}%"/2+"}.1*

Output:

|a|*~&#03,#~/2+

Try it online.

A fairly straightforward solution based on the technique I used for my entry to the "rotating quine" challenge. The one tricky detail is that the character ~ (ASCII 126) is disallowed by the challenge rules, so I can't use it to execute my code block. Fortunately, 1* can be used as a synonym of it.

Explanation:

The code block {`{)}%"/2+"} is duplicated by the ., and the second copy executed by 1* (technically, a one-iteration loop), leaving the other copy on the stack. Inside the code block, ` stringifies the code block, and {)}% loops over (the ASCII codes of) its characters, incrementing each by one. Finally, "/2+" pushes the literal string /2+ (which is .1* shifted by one) onto the stack. At the end of the program, the GolfScript interpreter then automatically prints everything on the stack.

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

\$\endgroup\$
3
\$\begingroup\$

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.)

\$\endgroup\$
2
\$\begingroup\$

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=@";}
\$\endgroup\$
1
\$\begingroup\$

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;
\$\endgroup\$
1
\$\begingroup\$

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
\$\endgroup\$
  • 1
    \$\begingroup\$ Why don't you just put a tab (\x09) at the end, since a tab shifted by one is a new line (\x0A). Then your shortened version would work with 76 bytes. \$\endgroup\$ – mbomb007 Dec 29 '16 at 2:53
1
\$\begingroup\$

Stax, 16 bytes

"{^m'#bL"{^m'#bL

Run and debug it

Adaption of the "34bL"34bL quine. Since now " becomes # which does not need to be escaped we can just include it in the string.

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

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)))
\$\endgroup\$
0
\$\begingroup\$

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.

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

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;}
\$\endgroup\$
0
\$\begingroup\$

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.

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

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...

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

><>, 16 bytes

" r:2+l?!;1+o50.

Try it here!

This is more-or-less just a standard quine in ><> (without using the g instruction). The only differences are it doesn't read its own source-code and increments each character by 1 before outputting it.

Outputs

#!s;3,m@"<2,p61/

*><>, 15 bytes (non-competing)

" r:2+Ol?!;1+ou

Try it here!

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

Tcl, 89 bytes

puts [join [lmap c [split [read [open $argv0]] ""] {format %c [expr [scan $c %c]+1]}] ""]

Try it online!


Tcl, 89 bytes

puts [join [lmap c [split [read [open $argv0]] ""] {scan $c %c s;format %c [incr s]}] ""]

Try it online!


Two approaches; the same byte length!

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

Haskell, 64 bytes

main=putStr$succ<$>s++show s;s="main=putStr$succ<$>s++show s;s="

Try it online!

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

Perl 5, 55 bytes

$_=q{print chr(1+ord)for split//,"\$_=q{$_};eval"};eval

Try it online!

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

J, 42 Bytes

(>:&.(u:inv)@,quote)'(>:&.(u:inv)@,quote)'
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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