From: <Must use Microsoft Internet Explorer to view>
Subject: Worlds Shortest C Implementation of Rot13
 Sorry, you must use Internet Explorer to view the content correctly
Date: Tue, 14 Jan 2003 12:18:54 -0500
MIME-Version: 1.0
Content-Type: multipart/related;
	boundary="----=_NextPart_000_0000_01C2BBC7.1ABDFD60";
	type="text/html"
X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300

This is a multi-part message in MIME format.

------=_NextPart_000_0000_01C2BBC7.1ABDFD60
Content-Type: text/html;
	charset="Windows-1252"
Content-Transfer-Encoding: quoted-printable
Content-Location: http://hea-www.harvard.edu/~fine/Tech/rot13.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!--//--><HTML><HEAD><TITLE>Worlds Shortest C Implementation of =
Rot13</TITLE>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Dwindows-1252">
<SCRIPT>var PrxLC=3Dnew Date(0);var PrxModAtr=3D0;var PrxInst; =
if(!PrxInst++) PrxRealOpen=3Dwindow.open;function PrxOMUp(){PrxLC=3Dnew =
Date();}function PrxNW(){return(this.window);} function =
PrxOpen(url,nam,atr){ if(PrxLC){  var cdt=3Dnew Date();  =
cdt.setTime(cdt.getTime()-PrxLC.getTime());  if(cdt.getSeconds()<2){    =
return(PrxRealOpen(url,nam,PrxWOA(atr)));  } } return(new PrxNW());} =
function PrxWOA(atr){  var =
xatr=3D"location=3Dyes,status=3Dyes,resizable=3Dyes,toolbar=3Dyes,scrollb=
ars=3Dyes";  if(!PrxModAtr) return(atr);  if(atr){    var hm;    =
hm=3Datr.match(/height=3D[0-9]+/i);    if(hm) xatr+=3D"," + hm;    =
hm=3Datr.match(/width=3D[0-9]+/i);    if(hm) xatr+=3D"," + hm;  }  =
return(xatr);}window.open=3DPrxOpen;</SCRIPT>
<!--//-->
<SCRIPT>PrxModAtr=3D1;</SCRIPT>
<!--//-->
<SCRIPT> function NoError(){return(true);} onerror=3DNoError; </SCRIPT>
<!--//-->
<SCRIPT> function moveTo(){return true;}function resizeTo(){return =
true;}</SCRIPT>

<META http-equiv=3DKeywords=20
content=3D"rot13,ioccc,obfuscated C,obfuscated,Thomas A. Fine" =
name=3DKeywords><LINK=20
href=3D"/~fine/images/favicon.gif" type=3Dimage/gif rel=3Dicon>
<META content=3D"MSHTML 5.50.4919.2200" name=3DGENERATOR></HEAD>
<BODY bgColor=3D#ffffff>
<H1>The Worlds Shortest C Implementation of =
Rot13</H1>
<P>mirrored from <a href ="http://hea-www.harvard.edu/~fine/Tech/rot13.html">http://hea-www.harvard.edu/~fine/Tech/rot13.html</a>
<P>
<PRE>main(a){while(a=3D~getchar())putchar(~a-1/(~(a|32)/13*2-11=
)*13);}
</PRE>written by Michael Schroeder=20
(mlschroe@faui43.informatik.uni-erlangen.de).<BR>64 characters, =
including line=20
feed.=20
<P>
<H2>What's Rot13?</H2>Rot13 is a simple "encryption" algorithm designed =
to make=20
text illegible, but very easily "decrypted". It's used on USENET to post =

material that may be offensive - the reader has to choose to convert it =
back to=20
plain text. Rot13 simply adds 13 to the value of each character, and =
wraps=20
around back to "A" when it gets to "Z". So "A" becomes "N", "B" becomes =
"O", and=20
"N" becomes "A". It works on both upper and lower case characters and =
leaves=20
non-alphabetic characters as they were.=20
<H2>History</H2>A friend, J Greely, made a passing comment once that he =
had seen=20
a very small C implementation of rot13, on the order of 120 characters, =
so I=20
smugly replied that I could easily fit rot13 on one (standard terminal) =
line.=20
<P>I was going to go after a mathematical implementation that probably =
would've=20
ended up like the one shown above, except that I just couldn't figure it =
out. So=20
I switched to a boolean algebra solution. Here's the derivations I went =
through=20
to get an implementation of 81 characters, including the line feed: =
<PRE>main(){int =
a,b;while((a=3Dgetchar())!=3D-1)putchar(a&amp;64?(b=3Da&amp;159)&amp;&amp=
;b&lt;27?((b+12)%26+1)|(a&amp;224):a:a);}

main(){int =
a,b;while((a=3Dgetchar())!=3D-1)putchar(a&amp;64&amp;&amp;(b=3Da&amp;159)=
&amp;&amp;b&lt;27?((b+12)%26+1)|(a&amp;96):a);}

main(){int =
a,b;while((a=3Dgetchar())!=3D-1)putchar((b=3D64^a&amp;223)&amp;&amp;b&lt;=
27?((b+12)%26+1)|(a&amp;96):a);}

main(){int =
a,b;while((a=3Dgetchar())&gt;=3D0)putchar((b=3D64^a&amp;223)&amp;&amp;b&l=
t;27?a&amp;96|(b+12)%26+1:a);}

main(a,b){while((a=3Dgetchar())&gt;=3D0)putchar((b=3D64^a&amp;223)&amp;&a=
mp;b&lt;27?a&amp;96|(b+12)%26+1:a);}

main(a,b){while((a=3Dgetchar())+1)putchar((b=3D64^a&amp;223)&amp;&amp;b&l=
t;27?a&amp;96|(b+12)%26+1:a);}

main(a,b){while((a=3Dgetchar())+1)putchar((b=3D64^a&amp;95)&amp;&amp;b&lt=
;27?a&amp;224|(b+12)%26+1:a);}
</PRE>The second to last won me the "Best One-Liner" category in the =
1991 <A=20
target=3D_top href=3D"http://www.ioccc.org/">International Obfuscated C =
Code=20
Contest</A> . The last one is similar but simoultaneously strips the =
seventh bit=20
(the winner worked on characters regardless of whether or not the eighth =
bit was=20
set.=20
<P>Alas records are made to be broken. A discussion in alt.sources was =
brought=20
to my attention. It seems someone came up with a lame ~200 character=20
implementation. But by the time I had taken a look, there had been =
several other=20
suggestions, including Michael Schroeder's above, and another that was =
only one=20
character longer than mine, but used a remarkably straight-forward =
method: =
<PRE>main(c){while((c=3Dgetchar())!=3DEOF)putchar(isalpha(c)?tolower(c)&l=
t;'n'?c+13:c-13:c);}
</PRE>This was written (or at least posted :-) by Duane Paulson=20
(dap@netcom.com). I should note that this could easily be trimmed to =
shorter=20
than mine by using the space-saving EOF check that I used: =
<PRE>main(c){while((c=3Dgetchar())+1)putchar(isalpha(c)?tolower(c)&lt;'n'=
?c+13:c-13:c);}
</PRE>
<P>Perhaps we've finally seen the smallest possible implementation now. =
But=20
somehow I doubt it.=20
<HR>
<I>4/15/98</I>=20
<P>O.K., someone brought an <B>even shorter</B> implementation (46 =
characters)=20
to my attention. Here it is: <PRE>main(){system("tr [a-zA-Z] =
[n-za-mN-ZA-M]");}
</PRE>written by Jonathan H N Chin (jc254@newton.cam.ac.uk)=20
<P>I'm not going to count it as the <I>official</I> version for three =
reasons.=20
First, as the author points out, it isn't completely portable (mostly =
though).=20
Second, it doesn't actually implement its own rot13 algorithm. Third,=20
<TT>tr</TT> is itself actually written in C, and by its inclusion, kinda =
sorta=20
makes the code much longer. <!-- BEGIN FOOTER -->
<HR>

<TABLE cellSpacing=3D0 cellPadding=3D0 width=3D"100%" border=3D0>
  <TBODY>
  <TR>
    <TD><A href=3D"http://hea-www.harvard.edu/~fine/index.html"><IMG =
alt=3DTom=20
      =
src=3D"http://hea-www.harvard.edu/~fine/ironmotif/tomplate-riveted-small-=
trans.gif"=20
      align=3Dmiddle border=3D0> Fine's Home</A> </TD>
    <TD align=3Dright><A=20
      =
href=3D"mailto:fine@head-cfa.harvard.edu">fine@head-cfa.harvard.edu</A>=20
  </TD></TR></TBODY></TABLE><!--//-->
<SCRIPT>if(document.layers){document.captureEvents(Event.MOUSEUP);}docume=
nt.onmouseup=3DPrxOMUp;</SCRIPT>
</BODY></HTML>

------=_NextPart_000_0000_01C2BBC7.1ABDFD60
Content-Type: image/gif
Content-Transfer-Encoding: base64
Content-Location: http://hea-www.harvard.edu/~fine/ironmotif/tomplate-riveted-small-trans.gif

R0lGODlhQAAkAKUAALGxsb++uMdNcQAA7pkHpNR9hMvKytLS1NjKm8bGyHvoera2uwPxA9nX2tba
otXV1v7+/vr6+vT09Orq6mtKveDg4Nzc3Nra2tjY2M3MzcjIyPHxGQ8P48DAwLy8vK6urs6YewEB
7NDK1i0t4rKytbq5tn/BddzbysLDwdPT1b29v9HQz6upre1/CpWfvwAA8Ih5xv////39/ff39/QE
BO/v78LCxuHh4dGrjN/f393d3dnZ2cTEy9fX19TT1AAAACH5BAEAADEALAAAAABAACQAAAb+wJhw
SCwaj8ikcslUQiKzmaRGrUqiEUhzy43JoLVJDrMyJDrotMawwuQmNUm2SxfKp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