45
\$\begingroup\$

Task

Read a string of characters and produce the ROT13 of it. All characters besides [a-zA-Z] should be output verbatim. Each letter in the output may be in any case you wish (you do not have to preserve it).

ROT13 is a simple substitution cipher where each letter of the alphabet is rotated by 13 places (it doesn't matter whether you do it forward or backward, since ROT13 is it's own inverse).

Examples

Input
Output

Hello, world!
Uryyb, jbeyq!

Code Golf and Coding Challenges is awesome.
Pbqr Tbys naq Pbqvat Punyyratrf vf njrfbzr.

Mod abuse!1!!1
Zbq nohfr!1!!1
\$\endgroup\$
13
  • 3
    \$\begingroup\$ The problem shouldn't be a tag, so I removed ROT13, just an FYI \$\endgroup\$ Commented Jan 27, 2011 at 21:18
  • 3
    \$\begingroup\$ Don't you mean A-Za-z (to count both upper- and lower-case) ? \$\endgroup\$
    – Joey Adams
    Commented Jan 27, 2011 at 21:33
  • 6
    \$\begingroup\$ @Chris Jester-Young, it belongs to this category at wikipedia. It's part of cryptography, just not the hardest one. Anyway, I'm not longer following this site. Community dissapointed me. Sorry. GL HF. \$\endgroup\$
    – Nakilon
    Commented Jan 30, 2011 at 18:59
  • 21
    \$\begingroup\$ Saying xor is not encryption is like saying a+b is not math. \$\endgroup\$
    – Nakilon
    Commented Jan 30, 2011 at 19:02
  • 3
    \$\begingroup\$ @Nakilon: Actually, I call that arithmetic, not mathematics. *shrug* \$\endgroup\$ Commented Jan 30, 2011 at 19:04

70 Answers 70

26
\$\begingroup\$

Bash, 23 bytes

Canonical 23-character answer:

tr A-Za-z N-ZA-Mn-za-m
\$\endgroup\$
6
  • 1
    \$\begingroup\$ I'm without access to bash at the moment, but I think this should work: tr A-za-m N-ZA-z (16 chars) \$\endgroup\$
    – Nabb
    Commented Feb 2, 2011 at 7:24
  • 2
    \$\begingroup\$ @Nabb: Nice to see you, GolfScript-meister! :-D I think your solution here would break the stipulation that "All characters besides A-Z should be copied to the output verbatim". \$\endgroup\$ Commented Feb 2, 2011 at 12:07
  • \$\begingroup\$ @Chris: Yep, looks like you're right. \$\endgroup\$
    – Nabb
    Commented Feb 3, 2011 at 2:22
  • \$\begingroup\$ @Nabb: No, to me it looks as if he's wrong. Or may you show an example? \$\endgroup\$ Commented Feb 6, 2011 at 5:56
  • 3
    \$\begingroup\$ @user unknown: Type in [\\]^_` in the input. It will come back as NOPQRS rather than [\\]^_`, at least in the version of tr I have. (Those are the six characters in ASCII that lie between Z and a. Obviously, all other characters will work correctly.) \$\endgroup\$ Commented Feb 6, 2011 at 7:07
22
\$\begingroup\$

Python 2, 34 bytes

print raw_input().encode('rot13')
\$\endgroup\$
8
  • 1
    \$\begingroup\$ -1 I think it is cheating that you are using a built in library. \$\endgroup\$ Commented Jan 27, 2011 at 21:23
  • 20
    \$\begingroup\$ I've used them in every code golf I took part in... Also if using python's standard lib is cheating, how is using tr not? \$\endgroup\$
    – Champo
    Commented Jan 27, 2011 at 21:27
  • 3
    \$\begingroup\$ @Anon In any case, it's an anwser you just ignore.. It's not off target, there's not any rules set by the OP nor by the community. Sure, maybe mine is not an award winning solution, like the one using tr that kicks ass. But it's not a joke, I'm leveraging the power of python to reduce the count, just like any other would do. \$\endgroup\$
    – Champo
    Commented Jan 27, 2011 at 21:37
  • 3
    \$\begingroup\$ @Glenn: on the contrary, I have never seen a code-golf thing that say so. Neither do codegolf.com, golf.shinh.org or SPOJ SHORTEN. \$\endgroup\$
    – hallvabo
    Commented Jan 27, 2011 at 23:36
  • 11
    \$\begingroup\$ @Glenn, I think library functions are fair game. It's up to the golf question to be interesting enough to not be in anyone's library or to specifically exclude. \$\endgroup\$
    – gnibbler
    Commented Jan 28, 2011 at 2:09
21
\$\begingroup\$

Bash - 5 chars

rot13

 

\$\endgroup\$
7
  • 22
    \$\begingroup\$ Two downvotes so far (with no comments), but no downvotes for the question. I assume that means it's ok to ask trivial codegolfs but not to give the trivial answer. Harder codegolfs please! \$\endgroup\$
    – gnibbler
    Commented Jan 30, 2011 at 20:49
  • 11
    \$\begingroup\$ Which bash-version? I don't have a build-in rot13. bash --version GNU bash, Version 4.0.33(1)-release (i486-pc-linux-gnu) \$\endgroup\$ Commented Feb 6, 2011 at 5:21
  • 15
    \$\begingroup\$ I'd have submitted this as rot13 - 0 chars... ;) \$\endgroup\$
    – boothby
    Commented Mar 9, 2013 at 18:10
  • 4
    \$\begingroup\$ erm... no. my bash (Ubuntu 13.10) says "command not found, you can install this program by sudo apt-get install bsdgames" \$\endgroup\$
    – user16402
    Commented Apr 9, 2014 at 17:43
  • 4
    \$\begingroup\$ rot13 is not in coreutils, so this isn't a bash solution or a bash+coreutils solution. \$\endgroup\$
    – Chris
    Commented Feb 27, 2019 at 17:53
17
\$\begingroup\$

Befunge - 7x30 = 210 6x26 = 156 characters

New streaming version that supports both upper and lower case and should support unlimited input.

v,<               <<     <
  ^-4-9<    >:"A"\`|
     >:"a"\`|     #>:"Z"`|
>~:0`| #    >:"z"`|
,    @ |    <`"m":<v`"M":<
^  +4+9<    ^      <

The old version

This stores the values inside its own source code. Really shows off how horrible it is to try and output stored values in the same order that you receive them. Only supports lowercase characters.

vp0p11:+1g11<      < <
v                    ^-4-9<
v    >:"a"\`|>:"z"`|>:"m"`|
>~:0`|      >^     >^#
                     ^+4+9<
     >$011g1+0p>12g1+:12p0g:v
               ^           ,_@

I'm not sure exactly what limitations this has, using http://www.quirkster.com/iano/js/befunge.html as the interpreter it does appear to break with large inputs.

\$\endgroup\$
1
  • \$\begingroup\$ You can remove the whitespace at the end of lines. \$\endgroup\$
    – Adalynn
    Commented Dec 31, 2017 at 18:59
11
\$\begingroup\$

Ruby - 60 57 38 37 chars

Edit: And just realised Ruby strings have a tr method.

puts$<.read.tr'A-Za-z','N-ZA-Mn-za-m'

Test

input = "The challenge: To read an input of arbitrary length and produce the ROT13 of the input. All characters besides A-Z should be copied to the output verbatim, and case should be preserved if possible.

Any language that can read and write standard streams is fair game."

output = `echo '#{input}' | ruby golf-rot13.rb`

puts "Input:"
puts input
puts "-------"
puts "Output:"
puts output

Gives:

Input:
The challenge: To read an input of arbitrary length and produce the ROT13 of the input. All characters besides A-Z should be copied to the output verbatim, and case should be preserved if possible.

Any language that can read and write standard streams is fair game.
-------
Output:
Gur punyyratr: Gb ernq na vachg bs neovgenel yratgu naq cebqhpr gur EBG13 bs gur vachg. Nyy punenpgref orfvqrf N-M fubhyq or pbcvrq gb gur bhgchg ireongvz, naq pnfr fubhyq or cerfreirq vs cbffvoyr.

Nal ynathntr gung pna ernq naq jevgr fgnaqneq fgernzf vf snve tnzr.
\$\endgroup\$
6
  • \$\begingroup\$ You don't need the space after puts, and 'A-z' is a shortcut for 'A-Za-z' \$\endgroup\$
    – Ventero
    Commented Feb 2, 2011 at 14:52
  • 1
    \$\begingroup\$ @Ventro: Thanks, after a bit of testing it seems 'A-z' is actually 'A-Z[\]^_a-z', damn ascii having characters between Z` and a. \$\endgroup\$
    – Nemo157
    Commented Feb 2, 2011 at 20:50
  • 1
    \$\begingroup\$ Essentially the same but 35 characters: puts gets.tr'A-Za-z','N-ZA-Mn-za-m'. \$\endgroup\$ Commented Mar 13, 2011 at 23:43
  • \$\begingroup\$ @Michael: Except gets only returns the first line, using $<.read reads until EOF. The question doesn't say anything about whether the input can contain new lines so I just erred on the side of caution. \$\endgroup\$
    – Nemo157
    Commented Mar 14, 2011 at 0:22
  • 1
    \$\begingroup\$ Fair enough, but since the exercise specification only mentioned "arbitrary length" but said nothing about newlines, I'd rather err on the side of brevity in codegolf ;-) \$\endgroup\$ Commented Mar 14, 2011 at 7:32
10
\$\begingroup\$

vim, 5 keystrokes

Assuming normal mode and that the text is already written in the buffer:

ggg?G

Or, fallowing vimgolf's conventions:

g?GZZ

You can also invoke it as a terminal command, something like this:

$ vim -c 'norm g?G' -
< your text here ...
... multiple lines if you want ...
... terminate input with ctrl+D on a blank line >

I guess the latter would count as a "program" of 8 characters (norm g?G)

\$\endgroup\$
3
  • \$\begingroup\$ norm g?G is short for normal g?G that makes 8 chars. \$\endgroup\$ Commented Sep 4, 2014 at 15:23
  • \$\begingroup\$ I think you can safely assume to start out on line 1, so the first gg can be left off. I would say 3 keystrokes when the file is open. \$\endgroup\$ Commented Sep 4, 2014 at 15:24
  • 1
    \$\begingroup\$ If we use vimgolf's conventions (you start in a vanilla vim having just opened the file, but need to save and quit) we also get 5 (g?GZZ). \$\endgroup\$
    – FireFly
    Commented Oct 30, 2014 at 21:53
10
\$\begingroup\$

R, 37 bytes

example("chartr");cat(rot(scan(,"")))

example("chartr") runs the examples for chartr, which includes the rot function, which is ROT13 by default....

\$\endgroup\$
7
\$\begingroup\$

C - 83 79 characters

main(c,b){while((c=getchar())>=0)b=c&96,putchar(isalpha(c)?b+1+(c-b+12)%26:c);}

Readable version:

#include <ctype.h>
#include <stdio.h>

int main(void)
{
    int c, base;

    while ((c = getchar()) >= 0) {
        if (isalpha(c)) {
            base = (c & 96) + 1; /* First letter of the upper or lower case. */
            c = base + (c - base + 13) % 26;
        }
        putchar(c);
    }

    return 0;
}
\$\endgroup\$
8
  • 1
    \$\begingroup\$ Are you including the headers you include in your count? \$\endgroup\$
    – JPvdMerwe
    Commented Jan 27, 2011 at 22:18
  • 1
    \$\begingroup\$ @JPvdMerwe: I didn't include any headers in the golfed version, nor did I need to. \$\endgroup\$
    – Joey Adams
    Commented Jan 27, 2011 at 22:34
  • \$\begingroup\$ You can use the coma operator before putchar to remove a pair of braces. \$\endgroup\$
    – Alexandru
    Commented Jan 27, 2011 at 23:05
  • \$\begingroup\$ Could you explain main(c,b)? It's the first time i see this. \$\endgroup\$
    – Alexandru
    Commented Jan 27, 2011 at 23:11
  • 2
    \$\begingroup\$ @Alexandru most C compilers support main with any parameters. Also, the original C standard defines that an argument with no type is an int. Thus you get to declare ints without actually writing int. \$\endgroup\$
    – Champo
    Commented Jan 27, 2011 at 23:31
7
\$\begingroup\$

Python (117 bytes)

Here's a Python version that avoids the rot13()-method.

import sys
print"".join([chr(x/32*32+1+(x%32+12)%26if 64<x<91or 96<x<123 else x)for x in map(ord,sys.stdin.read())])
\$\endgroup\$
4
  • \$\begingroup\$ raw_input returns one line not all input. \$\endgroup\$
    – Alexandru
    Commented Jan 27, 2011 at 23:34
  • \$\begingroup\$ you need to import sys and use sys.stdin.read(). \$\endgroup\$
    – Alexandru
    Commented Jan 27, 2011 at 23:41
  • \$\begingroup\$ @Alexandru: will do \$\endgroup\$
    – JPvdMerwe
    Commented Jan 27, 2011 at 23:46
  • \$\begingroup\$ -2 bytes if you eliminate the [] to make the list comp a generator: tio.run/… \$\endgroup\$
    – moltarze
    Commented Jul 17, 2019 at 16:55
7
\$\begingroup\$

tr/// solution in Perl (39 characters), boilerplate can be removed with -p:

while(<>){y/a-zA-Z/n-za-mN-ZA-M/;print}

Using -p (23 characters including the extra switch):

perl -pe'y/a-zA-Z/n-za-mN-ZA-M/'
\$\endgroup\$
1
  • \$\begingroup\$ Add 1 char for the p, but please remove the boilerplate! \$\endgroup\$
    – J B
    Commented Feb 6, 2011 at 21:04
6
\$\begingroup\$

DC (111 108 for the dc itself)

Ok, here it is in (mostly) dc and some sed and od magic to get it into the right format for the code. If you don't count the input thing (echo -n MESSAGE |) it's 160 bytes:

od -An -t dC|sed 's/^\ *//;s/\ \{2,3\}/\n/g'|dc -e'[13+26%]sm[65-lmx65+]su[97-lmx97+]sl[96<b64<dPc]sa[91>c]sd[123>e]sb[lux]sc[llxdd]se[ddddlaxlrx]sy[?z0<y]dsrx'

As a point of interest, the dc programme itself is only a 108 bytes long, shorter than the non-library python version. It even preserves case and punctuation, and beats Javascript in the above submission! If only I could better parse the output of od, or better yet replace it altogether.

EDIT: It's worth noting that the question doesn't indicate a trailing new line 10P which saves me three further bytes.

EDIT 2: There's no specification for the format of the input, so I assume it's taken in as is convenient for my programme :P

\$\endgroup\$
5
\$\begingroup\$

Perl6 (54)

$*IN.lines».trans("a..zA..Z"=>"n..za..mN..ZA..M").say
\$\endgroup\$
5
\$\begingroup\$

PHP - 103 98 80 characters

(not using str_rot13())

<?=preg_replace('#[a-zA-Z]#e','chr(($x=ord("\0"))-$x%32+1+($x%32+12)%26)',`cat`);
\$\endgroup\$
5
\$\begingroup\$

Delphi, 110

var c:Char;begin repeat Read(c);Write(Chr(Ord(c)+(Ord(c in['A'..'M'])-Ord(c in['N'..'Z']))*13));until EOF;end.
\$\endgroup\$
2
  • \$\begingroup\$ var c:Char;begin repeat Read(c);Write(Chr(Ord(c)+(Ord(c in['A'..'M'])-Ord(c in['N'..'Z']))*13));until EOF;end. saves one character :) \$\endgroup\$ Commented Mar 13, 2011 at 21:36
  • \$\begingroup\$ @Wouter van Nifterick : Good spot! I'll update it accordingly \$\endgroup\$
    – PatrickvL
    Commented Mar 13, 2011 at 23:28
5
\$\begingroup\$

Haskell, 100 characters

a%b=([a..b]++)
main=interact$map$toEnum.((0%64$78%90$65%77$91%96$110%122$97%109$[123..])!!).fromEnum
\$\endgroup\$
5
\$\begingroup\$

CHIQRSX9+, 1

R

You just have to use the right tool for the problem.
CHIQRSX9+ is Turing complete, and it can read and write from standard channels with C.

\$\endgroup\$
9
  • 5
    \$\begingroup\$ @nyuszika7h The language was invented before this question was written, so it is valid. \$\endgroup\$ Commented Jan 2, 2014 at 20:25
  • 1
    \$\begingroup\$ It's still one of the things it was invented for, though. Seems like cheating to me. \$\endgroup\$
    – user344
    Commented Jan 2, 2014 at 21:11
  • 8
    \$\begingroup\$ @nyuszika7h And golfscript was invented to win in code-golf challenges. Do you think that is cheating too? \$\endgroup\$ Commented Jan 2, 2014 at 21:12
  • 2
    \$\begingroup\$ @Mego It's hardly fair to apply those standards here when they were written a year after this answer. \$\endgroup\$
    – Doorknob
    Commented Nov 14, 2015 at 19:32
  • 2
    \$\begingroup\$ MetaGolfScript is forbidden by a different rule \$\endgroup\$
    – user45941
    Commented Nov 15, 2015 at 14:48
5
\$\begingroup\$

LispLisp (16,636)

I'm sorry.

((((lisp (lisp (lisp (lisp lisp)))) (lisp lisp))
  (lisp lisp))
 (((lisp (lisp (lisp (lisp lisp))))
   ((lisp (lisp (lisp lisp)))
    ((lisp (lisp (lisp (lisp lisp))))
     (((lisp (lisp (lisp (lisp lisp))))
       ((lisp (lisp (lisp lisp)))
        (((lisp (lisp (lisp (lisp lisp))))
          (((lisp (lisp (lisp (lisp lisp))))
            ((lisp (lisp (lisp lisp)))
             (lisp (lisp (lisp (lisp lisp))))))
           (((lisp (lisp (lisp (lisp lisp))))
             ((lisp (lisp (lisp lisp)))
              (lisp (lisp (lisp lisp)))))
            (((lisp (lisp (lisp (lisp lisp))))
              ((lisp (lisp (lisp lisp)))
               (lisp (lisp (lisp (lisp lisp))))))
             (((lisp (lisp (lisp (lisp lisp))))
               ((lisp (lisp (lisp lisp)))
                ((lisp (lisp (lisp (lisp lisp)))) (lisp lisp))))
              (lisp (lisp (lisp lisp))))))))
         ((lisp (lisp (lisp lisp)))
          (lisp (lisp (lisp lisp)))))))
      (((lisp (lisp (lisp (lisp lisp))))
        ((lisp (lisp (lisp lisp)))
         (((lisp (lisp (lisp (lisp lisp))))
           (((lisp (lisp (lisp (lisp lisp))))
             ((lisp (lisp (lisp lisp)))
              (((lisp (lisp (lisp (lisp lisp)))) (lisp lisp))
               ((lisp (lisp (lisp lisp)))
                (lisp (lisp (lisp lisp)))))))
            (((lisp (lisp (lisp (lisp lisp))))
              (((lisp (lisp (lisp (lisp lisp)))) (lisp lisp))
               ((lisp (lisp (lisp lisp)))
                (((lisp (lisp (lisp (lisp lisp)))) (lisp lisp))
                 ((lisp (lisp (lisp lisp)))
                  ((lisp (lisp (lisp lisp))) (lisp lisp)))))))
             ((lisp (lisp (lisp lisp)))
              (((((lisp (lisp (lisp (lisp lisp))))
                  (((lisp (lisp (lisp (lisp lisp))))
                    ((lisp (lisp (lisp lisp)))
                     (lisp (lisp (lisp (lisp lisp))))))
                   (lisp (lisp (lisp lisp)))))
                 ((((lisp (lisp (lisp (lisp lisp))))
                    ((lisp (lisp (lisp (lisp lisp))))
                     (((lisp (lisp (lisp (lisp lisp))))
                       ((lisp (lisp (lisp lisp)))
                        (lisp (lisp (lisp (lisp lisp))))))
                      (lisp (lisp (lisp lisp))))))
                   (((lisp (lisp (lisp (lisp lisp)))) (lisp lisp))
                    (lisp lisp)))
                  (((lisp (lisp (lisp (lisp lisp))))
                    (((lisp (lisp (lisp (lisp lisp))))
                      ((lisp (lisp (lisp lisp)))
                       (lisp (lisp (lisp (lisp lisp))))))
                     (lisp (lisp (lisp lisp)))))
                   (lisp lisp))))
                (((lisp (lisp (lisp (lisp lisp))))
                  ((lisp (lisp (lisp lisp)))
                   ((lisp (lisp (lisp (lisp lisp))))
                    (((lisp (lisp (lisp (lisp lisp)))) (lisp lisp))
                     ((lisp (lisp (lisp lisp))) (lisp lisp))))))
                 (lisp (lisp (lisp lisp)))))
               ((((lisp (lisp (lisp (lisp lisp)))) (lisp lisp))
                 (((lisp (lisp (lisp (lisp lisp))))
                   ((lisp (lisp (lisp lisp)))
                    ((((lisp (lisp (lisp (lisp lisp))))
                       (((lisp (lisp (lisp (lisp lisp))))
                         ((lisp (lisp (lisp lisp)))
                          (lisp (lisp (lisp (lisp lisp))))))
                        (lisp (lisp (lisp lisp)))))
                      (lisp lisp))
                     ((((lisp (lisp (lisp (lisp lisp))))
                        (((lisp (lisp (lisp (lisp lisp))))
                          ((lisp (lisp (lisp lisp)))
                           (lisp (lisp (lisp (lisp lisp))))))
                         (lisp (lisp (lisp lisp)))))
                       (((lisp (lisp (lisp (lisp lisp))))
                         (((lisp (lisp (lisp (lisp lisp))))
                           ((lisp (lisp (lisp lisp)))
                            (lisp (lisp (lisp (lisp lisp))))))
                          (lisp (lisp (lisp lisp)))))
                        (lisp lisp)))
                      (((lisp (lisp (lisp (lisp lisp))))
                        ((lisp (lisp (lisp lisp)))
                         ((lisp (lisp (lisp (lisp lisp))))
                          (((lisp (lisp (lisp (lisp lisp)))) (lisp lisp))
                           ((lisp (lisp (lisp lisp))) (lisp lisp))))))
                       (lisp (lisp (lisp lisp))))))))
                  (((lisp (lisp (lisp (lisp lisp)))) (lisp lisp))
                   ((lisp (lisp (lisp lisp)))
                    ((((lisp (lisp (lisp (lisp lisp)))) (lisp lisp))
                      (lisp lisp))
                     (((lisp (lisp (lisp (lisp lisp))))
                       ((lisp (lisp (lisp lisp)))
                        (((lisp (lisp (lisp (lisp lisp))))
                          ((lisp (lisp (lisp lisp)))
                           ((lisp (lisp (lisp (lisp lisp))))
                            (((lisp (lisp (lisp (lisp lisp)))) (lisp lisp))
                             ((lisp (lisp (lisp lisp))) (lisp lisp))))))
                         (lisp (lisp (lisp lisp))))))
                      (((lisp (lisp (lisp (lisp lisp)))) (lisp lisp))
                       (lisp lisp))))))))
                (((lisp (lisp (lisp (lisp lisp))))
                  ((lisp (lisp (lisp lisp)))
                   ((((lisp (lisp (lisp (lisp lisp))))
                      (((lisp (lisp (lisp (lisp lisp))))
                        ((lisp (lisp (lisp lisp)))
                         (lisp (lisp (lisp (lisp lisp))))))
                       (lisp (lisp (lisp lisp)))))
                     (((lisp (lisp (lisp (lisp lisp))))
                       ((lisp (lisp (lisp lisp)))
                        (((lisp (lisp (lisp (lisp lisp))))
                          (((lisp (lisp (lisp (lisp lisp))))
                            ((lisp (lisp (lisp lisp)))
                             (lisp (lisp (lisp (lisp lisp))))))
                           (lisp (lisp (lisp lisp)))))
                         (((lisp (lisp (lisp (lisp lisp))))
                           (((lisp (lisp (lisp (lisp lisp))))
                             ((lisp (lisp (lisp lisp)))
                              (lisp (lisp (lisp (lisp lisp))))))
                            (lisp (lisp (lisp lisp)))))
                          (lisp lisp)))))
                      ((((lisp (lisp (lisp (lisp lisp)))) (lisp lisp))
                        (lisp lisp))
                       (((lisp (lisp (lisp (lisp lisp))))
                         (((lisp (lisp (lisp (lisp lisp))))
                           ((lisp (lisp (lisp lisp)))
                            (lisp (lisp (lisp (lisp lisp))))))
                          (lisp (lisp (lisp lisp)))))
                        (lisp lisp)))))
                    (((lisp (lisp (lisp (lisp lisp))))
                      ((lisp (lisp (lisp lisp)))
                       ((lisp (lisp (lisp (lisp lisp))))
                        (((lisp (lisp (lisp (lisp lisp)))) (lisp lisp))
                         ((lisp (lisp (lisp lisp)))
                          ((((lisp (lisp (lisp (lisp lisp))))
                             (((lisp (lisp (lisp (lisp lisp))))
                               ((lisp (lisp (lisp lisp)))
                                (lisp (lisp (lisp (lisp lisp))))))
                              (lisp (lisp (lisp lisp)))))
                            (((lisp (lisp (lisp (lisp lisp))))
                              ((lisp (lisp (lisp lisp)))
                               (((lisp (lisp (lisp (lisp lisp))))
                                 (((lisp (lisp (lisp (lisp lisp))))
                                   ((lisp (lisp (lisp lisp)))
                                    (lisp (lisp (lisp (lisp lisp))))))
                                  (lisp (lisp (lisp lisp)))))
                                (((lisp (lisp (lisp (lisp lisp))))
                                  (((lisp (lisp (lisp (lisp lisp))))
                                    ((lisp (lisp (lisp lisp)))
                                     (lisp (lisp (lisp (lisp lisp))))))
                                   (lisp (lisp (lisp lisp)))))
                                 (lisp lisp)))))
                             ((((lisp (lisp (lisp (lisp lisp)))) (lisp lisp))
                               (lisp lisp))
                              (((lisp (lisp (lisp (lisp lisp))))
                                (((lisp (lisp (lisp (lisp lisp))))
                                  ((lisp (lisp (lisp lisp)))
                                   (lisp (lisp (lisp (lisp lisp))))))
                                 (lisp (lisp (lisp lisp)))))
                               (lisp lisp)))))
                           ((lisp (lisp (lisp (lisp lisp))))
                            (((lisp (lisp (lisp (lisp lisp))))
                              ((lisp (lisp (lisp lisp)))
                               (lisp (lisp (lisp (lisp lisp))))))
                             (lisp (lisp (lisp lisp)))))))))))
                     (lisp (lisp (lisp lisp)))))))
                 ((((lisp (lisp (lisp (lisp lisp))))
                    (((lisp (lisp (lisp (lisp lisp))))
                      ((lisp (lisp (lisp lisp)))
                       (lisp (lisp (lisp (lisp lisp))))))
                     (lisp (lisp (lisp lisp)))))
                   (((lisp (lisp (lisp (lisp lisp))))
                     ((lisp (lisp (lisp lisp)))
                      (((lisp (lisp (lisp (lisp lisp))))
                        (((lisp (lisp (lisp (lisp lisp))))
                          ((lisp (lisp (lisp lisp)))
                           (lisp (lisp (lisp (lisp lisp))))))
                         (lisp (lisp (lisp lisp)))))
                       (((lisp (lisp (lisp (lisp lisp))))
                         (((lisp (lisp (lisp (lisp lisp))))
                           ((lisp (lisp (lisp lisp)))
                            (lisp (lisp (lisp (lisp lisp))))))
                          (lisp (lisp (lisp lisp)))))
                        (lisp lisp)))))
                    ((((lisp (lisp (lisp (lisp lisp)))) (lisp lisp))
                      (lisp lisp))
                     (((lisp (lisp (lisp (lisp lisp))))
                       (((lisp (lisp (lisp (lisp lisp))))
                         ((lisp (lisp (lisp lisp)))
                          (lisp (lisp (lisp (lisp lisp))))))
                        (lisp (lisp (lisp lisp)))))
                      (lisp lisp)))))
                  (((lisp (lisp (lisp (lisp lisp))))
                    ((lisp (lisp (lisp lisp)))
                     ((lisp (lisp (lisp (lisp lisp))))
                      (((lisp (lisp (lisp (lisp lisp)))) (lisp lisp))
                       ((lisp (lisp (lisp lisp)))
                        (((lisp (lisp (lisp (lisp lisp))))
                          ((lisp (lisp (lisp lisp)))
                           (((lisp (lisp (lisp (lisp lisp)))) (lisp lisp))
                            ((lisp (lisp (lisp lisp)))
                             (lisp (lisp (lisp lisp)))))))
                         (((lisp (lisp (lisp (lisp lisp))))
                           (((lisp (lisp (lisp (lisp lisp)))) (lisp lisp))
                            ((lisp (lisp (lisp lisp)))
                             (((lisp (lisp (lisp (lisp lisp)))) (lisp lisp))
                              ((lisp (lisp (lisp lisp)))
                               ((lisp (lisp (lisp lisp))) (lisp lisp)))))))
                          ((lisp (lisp (lisp lisp)))
                           (((((lisp (lisp (lisp (lisp lisp))))
                               (((lisp (lisp (lisp (lisp lisp))))
                                 ((lisp (lisp (lisp lisp)))
                                  (lisp (lisp (lisp (lisp lisp))))))
                                (lisp (lisp (lisp lisp)))))
                              (((lisp (lisp (lisp (lisp lisp))))
                                ((lisp (lisp (lisp lisp)))
                                 (((lisp (lisp (lisp (lisp lisp))))
                                   (((lisp (lisp (lisp (lisp lisp))))
                                     ((lisp (lisp (lisp lisp)))
                                      (lisp (lisp (lisp (lisp lisp))))))
                                    (lisp (lisp (lisp lisp)))))
                                  (((lisp (lisp (lisp (lisp lisp))))
                                    (((lisp (lisp (lisp (lisp lisp))))
                                      ((lisp (lisp (lisp lisp)))
                                       (lisp (lisp (lisp (lisp lisp))))))
                                     (lisp (lisp (lisp lisp)))))
                                   (lisp lisp)))))
                               ((((lisp (lisp (lisp (lisp lisp)))) (lisp lisp))
                                 (lisp lisp))
                                (((lisp (lisp (lisp (lisp lisp))))
                                  (((lisp (lisp (lisp (lisp lisp))))
                                    ((lisp (lisp (lisp lisp)))
                                     (lisp (lisp (lisp (lisp lisp))))))
                                   (lisp (lisp (lisp lisp)))))
                                 (lisp lisp)))))
                             (((lisp (lisp (lisp (lisp lisp))))
                               ((lisp (lisp (lisp lisp)))
                                ((lisp (lisp (lisp (lisp lisp))))
                                 (((lisp (lisp (lisp (lisp lisp))))
                                   (lisp lisp))
                                  ((lisp (lisp (lisp lisp))) (lisp lisp))))))
                              (lisp (lisp (lisp lisp)))))
                            (((((lisp (lisp (lisp (lisp lisp)))) (lisp lisp))
                               (lisp lisp))
                              (((lisp (lisp (lisp (lisp lisp))))
                                ((lisp (lisp (lisp lisp)))
                                 ((lisp (lisp (lisp (lisp lisp))))
                                  (((lisp (lisp (lisp (lisp lisp))))
                                    ((lisp (lisp (lisp lisp)))
                                     (lisp (lisp (lisp (lisp lisp))))))
                                   (((lisp (lisp (lisp (lisp lisp))))
                                     ((lisp (lisp (lisp lisp)))
                                      ((lisp (lisp (lisp (lisp lisp))))
                                       (lisp lisp))))
                                    (lisp (lisp (lisp lisp))))))))
                               (((lisp (lisp (lisp (lisp lisp))))
                                 ((lisp (lisp (lisp lisp)))
                                  ((lisp (lisp (lisp (lisp lisp))))
                                   ((lisp (lisp (lisp lisp)))
                                    (lisp (lisp (lisp lisp)))))))
                                (((lisp (lisp (lisp (lisp lisp))))
                                  (((lisp (lisp (lisp (lisp lisp))))
                                    ((lisp (lisp (lisp lisp)))
                                     (lisp (lisp (lisp (lisp lisp))))))
                                   (((lisp (lisp (lisp (lisp lisp))))
                                     ((lisp (lisp (lisp lisp)))
                                      (lisp (lisp (lisp lisp)))))
                                    (((lisp (lisp (lisp (lisp lisp))))
                                      (lisp lisp))
                                     (lisp lisp)))))
                                 ((lisp (lisp (lisp lisp)))
                                  ((lisp (lisp (lisp (lisp lisp))))
                                   (((lisp (lisp (lisp (lisp lisp))))
                                     ((lisp (lisp (lisp lisp)))
                                      (lisp (lisp (lisp (lisp lisp))))))
                                    (lisp (lisp (lisp lisp))))))))))
                             ((lisp (lisp (lisp lisp))) (lisp lisp))))))))))))
                   (lisp (lisp (lisp lisp))))))))))))
          (lisp lisp))))
       (((lisp (lisp (lisp (lisp lisp)))) (lisp lisp))
        ((lisp (lisp (lisp lisp)))
         (lisp (lisp (lisp lisp))))))))))
  (((lisp (lisp (lisp (lisp lisp))))
    (((lisp (lisp (lisp (lisp lisp))))
      ((lisp (lisp (lisp lisp)))
       (lisp (lisp (lisp (lisp lisp))))))
     (((lisp (lisp (lisp (lisp lisp))))
       ((lisp (lisp (lisp lisp)))
        (lisp (lisp (lisp lisp)))))
      (((lisp (lisp (lisp (lisp lisp)))) (lisp lisp))
       (lisp lisp)))))
   ((lisp (lisp (lisp lisp)))
    (((lisp (lisp (lisp (lisp lisp)))) (lisp lisp))
     ((lisp (lisp (lisp lisp)))
      ((lisp (lisp (lisp lisp))) (lisp lisp))))))))
\$\endgroup\$
6
5
\$\begingroup\$

C (clang), 200 164 113 86 bytes

main(){char*s,*t=s;for(gets(s);*s;s++)isalpha(*s)?*s+=(*s&~32)<78?13:-13:0;printf(t);}

Try it online!

This uses a for-loop which converts each letter into ROT13 using their ASCII values to use lesser bytes. scanf()'s parameters are now longer to include spaces in the string, since the previous code revision can't take spaces for input, so now it's longer.

Thanks to ceiling cat for golfing 36 bytes. Thanks to EasyasPi, with the help of ceilingcat, for golfing 51 bytes. Thanks to a stone arachnid for golfing 27 bytes.

\$\endgroup\$
3
  • 2
    \$\begingroup\$ Welcome to Code Golf Stack Exchange, Code Golf Stack Exchange! Nice first answer. I don't know C at all, but it looks like bitwise operators work to save bytes. Nice username as well. \$\endgroup\$
    – user100690
    Commented May 31, 2021 at 13:26
  • \$\begingroup\$ 151 bytes Only In Code Golf™ \$\endgroup\$
    – EasyasPi
    Commented Jun 4, 2021 at 5:57
  • \$\begingroup\$ 86 bytes using bitwise arithmetic and the isalpha() function from ctype.h \$\endgroup\$ Commented Jun 5, 2021 at 4:58
4
\$\begingroup\$

PHP - 41 Characters

while($r=fgets(STDIN))echo str_rot13($r);
\$\endgroup\$
1
  • 5
    \$\begingroup\$ -1 for rep-whoring. \$\endgroup\$
    – Raynos
    Commented Feb 3, 2011 at 21:39
4
\$\begingroup\$

Python 3 (107)

Ok, I promise to stop answering this question now, but I felt compelled to beat the DC answer in Python. This probably reflects poorly on me as a person :).

import sys;[print(x.isalpha()and chr((ord(x)&96)+1+(ord(x)%32+12)%26)or x,end='')for x in sys.stdin.read()]
\$\endgroup\$
4
\$\begingroup\$

C: 69 68 characters

Alright, I know this thread is long dead, but I couldn't stand the (long) C-solution which doesn't even compile on Clang (but does on GCC).

main(c){putchar(isalpha(c=getchar())*((c|32)<110?13:-13)+c);main();}

It is probably almost still squeezable. It certainly was squeezable. And not only was it squeezable, it was possible to make it recursive.

\$\endgroup\$
4
\$\begingroup\$

05AB1E, 13 12 bytes

Saved a byte thanks to robbie0630

ADu)øJD2äRJ‡

Try it online!

Explanation

ADu           # push lower-case and uppercase alphabet
   )øJ        # zip, flatten and join, producing aAbB..zZ
      D2äRJ   # split a copy in 2 pieces, reverse and join producing nNoO..mM
           ‡  # translate input by replacing members of the alphabet 
              # with the corresponding member of the rot-13 alphabet
              # implicitly display
\$\endgroup\$
2
  • \$\begingroup\$ I ran through this with --debug, and it seems like the ˜ is a no-op in this case and can be cut out. \$\endgroup\$
    – robbie
    Commented Feb 15, 2018 at 3:31
  • \$\begingroup\$ @robbie0630: True. No idea why I had that in. Thank you :) \$\endgroup\$
    – Emigna
    Commented Feb 15, 2018 at 6:50
4
\$\begingroup\$

x86-16 Machine Code, IBM PC DOS, 37 bytes

Binary:

00000000: b408 cd21 3c20 7e1c 8ad0 0c20 3c61 7c0e  ...!< ~.... <a|.
00000010: 3c7a 7f0a b60d 3c6e 7c02 b6f3 02d6 b402  <z....<n|.......
00000020: cd21 ebdc c3                             .!...

Build and test using xxd -r

Unassembled listing:

        START: 
B4 08       MOV  AH, 8          ; DOS API read char from STDIN (no echo) 
CD 21       INT  21H            ; read char into AL 
3C 20       CMP  AL, ' '        ; is any non-printable control char?
7E 1C       JL   EXIT           ; if so, end 
8A D0       MOV  DL, AL         ; save original char in DL 
0C 20       OR   AL, 'a'-'A'    ; lowercase the char 
3C 61       CMP  AL, 'a'        ; is char less than 'a'? 
7C 0E       JL   WRCHR          ; if so, do not convert 
3C 7A       CMP  AL, 'z'        ; is char greater than 'z'? 
7F 0A       JG   WRCHR          ; if so, do not convert 
B6 0D       MOV  DH, 'n'-'a'    ; add or subtract 13 
3C 6E       CMP  AL, 'n'        ; is char less than 'n'? 
7C 02       JL   ADD13          ; if so, add 13 
B6 F3       MOV  DH, 'a'-'n'    ; otherwise add -13 
        ADD13: 
02 D6       ADD  DL, DH         ; add 13 or -13
        WRCHR: 
B4 02       MOV  AH, 2          ; DOS API write char to STDOUT 
CD 21       INT  21H            ; write char in DL 
EB DC       JMP  START          ; keep looping 
        EXIT: 
C3           RET                ; return to DOS

As a standalone PC DOS executable program. Input is via STDIN, output to STDOUT.

enter image description here

Re-submitting answer to conform to STDIN challenge requirement.

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

Java 251 chars

public class r{public static void main(String[] a){String s = a[0];for(int i=0;i<s.length();){char c=s.charAt(i++);if(c>='a'&&c<='m')c+=13;else if(c>='n'&&c<='z')c-= 13;else if(c>='A'&&c<='M')c+=13;else if(c>='A'&&c <='Z')c-=13;System.out.print(c);}}}
\$\endgroup\$
3
\$\begingroup\$

Javascript (165)

a=prompt();y="ABCDEFGHIJKLMNOPQRSTUVWXYZ";b=y[s="substr"](13)+y[s](0,13);y+=y[l="toLowerCase"]();b+=b[l]();o='';for(i=0;a[i];i++)o+=b[y.indexOf(a[i])]||a[i];alert(o)

or (167) as previous Javascript solution, assuming readLine and print:

a=readLine();y="ABCDEFGHIJKLMNOPQRSTUVWXYZ";b=y[s="substr"](13)+y[s](0,13);y+=y[l="toLowerCase"]();b+=b[l]();o='';for(i=0;a[i];i++)o+=b[y.indexOf(a[i])]||a[i];print(o)
\$\endgroup\$
0
3
\$\begingroup\$

C, 136 bytes

I have never felt like any of my solutions are good enough to post on here, but made this for fun, and figured that it will be my gateway drug into code golf.

#define z(x) c>=x&&c<x+13
#define q(w) c>=w&&c<w+13
main(c){for(;(c=getchar())>=0;putchar(z('A')||z('a')?c+13:q('M')||q('m')?c-13:c));}
\$\endgroup\$
3
  • 4
    \$\begingroup\$ Welcome to Programming Puzzles and Code Golf Stack Exchange. Great first answer. And just for the record, we all feel that way sometimes, just know that anything that you create is "good enough" somewhere. \$\endgroup\$
    – GamrCorps
    Commented Feb 22, 2016 at 18:21
  • 2
    \$\begingroup\$ Everything here on PPCG is just for fun (or imaginary internet points) -- please, don't feel the work you've done to craft a solution isn't "good enough" \$\endgroup\$
    – cat
    Commented Feb 23, 2016 at 23:36
  • \$\begingroup\$ Thanks for all the support. I think posting something was all I needed to get my head in gear. I am going to try my hand at more of these challenges soon. \$\endgroup\$
    – Tormyst
    Commented Feb 24, 2016 at 2:10
3
\$\begingroup\$

><>, 49 bytes

0i::"{`"@(@)*84**-::"AZ"@(@)+:}?$"4"-2d*%"A"+{?$o

Try it online

Explanation

0i
On each loop we initialize the stack with 0 and an input character.
The 0 will be used as a dummy to perform rot-13 operations on when the input isn't a letter.

::"{`"@(@)*84**-
Convert lowercase letters into uppercase for easier future operations.

::"AZ"@(@)+:}?$
If the character isn't an uppercase letter, use the 0 instead.

"4"-2d*%"A"+
ROT-13 operation. Subtract 52, mod by 13 and add 65 to get an uppercase letter.

{?$
If the character wasn't an uppercase before the ROT-13 operations, use the saved character instead.

o
Print as a character.

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

Haskell - 112 characters

r l=drop 13 l++take 13 l
t=['a'..'z']
s=['A'..'Z']
main=interact$map(\x->maybe x id$lookup x$zip(t++s)$r t++r s)
\$\endgroup\$
2
\$\begingroup\$

JavaScript 1.8, 106

alert(prompt().replace(/\w/g,function(c)String.fromCharCode(c.charCodeAt()+(c.toLowerCase()<'n'?13:-13))))

JavaScript, 115

alert(prompt().replace(/\w/g,function(c){return String.fromCharCode(c.charCodeAt()+(c.toLowerCase()<'n'?13:-13))}))

This solution solves the problem by adding 13 to the character code if the character in question is in the first half of the alphabet, or subtracting 13 if it's in the second half.

\$\endgroup\$
1
  • \$\begingroup\$ You can save 7 characters by replacing +(c.toLowerCase()<'n'?13:-13)) with -13+26*/[a-m]/i.test(c). \$\endgroup\$
    – Jacob
    Commented Jun 11, 2014 at 6:15
2
\$\begingroup\$

Jolf, noncompeting (language postdates challenge)

Jolf, 3 bytes

Try it here!

,Ri
,R  rot13
  i the input

Or, with new implicit input, ,R.

I'm sorry, but I'm not. I implemented this because it appears often in code-breaking puzzles, which I use Jolf a lot for, and recently used in an ARG. \o/

A longer one (try that one here):

ρi"[%]"γpWpud.pWp2pu+t3 iγH
ρi                           regex replace input
  "[%]"                      "[...]" following
        pWpu                 "abc...xyzABC....XYZ"
       γ                     γ = that
            d                functional replace
             .         _iγH  the index on the alphabet that the element is
                p2pu         shift uppercase alphabet
                    +t3      over thirteen
              pW             upper + lower of that
\$\endgroup\$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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