34
\$\begingroup\$

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.

\$\endgroup\$
  • 3
    \$\begingroup\$ The problem shouldn't be a tag, so I removed ROT13, just an FYI \$\endgroup\$ – Nick Berardi Jan 27 '11 at 21:18
  • 2
    \$\begingroup\$ Don't you mean A-Za-z (to count both upper- and lower-case) ? \$\endgroup\$ – Joey Adams Jan 27 '11 at 21:33
  • 4
    \$\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 Jan 30 '11 at 18:59
  • 16
    \$\begingroup\$ Saying xor is not encryption is like saying a+b is not math. \$\endgroup\$ – Nakilon Jan 30 '11 at 19:02
  • 3
    \$\begingroup\$ Tags are used to categorize questions and help search similar questions. The cryptography tag (from Greek kryptós, "hidden, secret"; and graphein, "writing") in particular is for encryption and decryption problems. All encryption and decryption, not only those that are secure for modern applications. \$\endgroup\$ – Angs Dec 3 '16 at 10:33

52 Answers 52

1
\$\begingroup\$

JavaScript 135

s=prompt(),o='',i=0
while((c=s.charCodeAt(i++)))c+=c>64&&c<91?c<52?13:-13:c>96&&c<123?c<109?13:-13:0,o+=String.fromCharCode(c)
alert(o)
\$\endgroup\$
1
\$\begingroup\$

pyg-i - 76 Bytes

P(STDI.read().t({a:a-13 if(77<a<91)or a>109 else a+13 for a in M(ord,STl)}))

Python Equivient:

import sys,string
print(sys.stdin.read().translate({a:a-13 if(77<a<91)or a>109 else a+13 for a in map(ord,string.ascii_letters)}))
\$\endgroup\$
  • \$\begingroup\$ Invalid because the interpreter is not available. \$\endgroup\$ – cat Feb 22 '16 at 21:29
  • \$\begingroup\$ @cat Somehow I got the url wrong. It is fixed now. \$\endgroup\$ – Ian D. Scott Feb 23 '16 at 23:24
1
\$\begingroup\$

Python: 60 bytes

lambda s:''.join([chr((ord(c.lower())-84)%26+97)for c in s])

The only thing I don't like is having that .lower() in there. Any suggestions?

edit: I can get rid of the .lower(), and handle capitals better, but it's 94 bytes now:

lambda s:''.join([chr((ord(c)-(52if ord(c)<97else 84))%26+(65if ord(c)<97else 97))for c in s])
\$\endgroup\$
1
\$\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\$
1
\$\begingroup\$

C#, 94 bytes

s=>{var r="";foreach(var c in s)r+=(char)(Char.IsLetter(c)?(c|32)<110?c+13:c-13:c);return r;};

Anonymous function which returns the string of characters rotated by 13 places, based on this C answer from Fors. Works for any lower or upper letters while keeping other input the same.

Full program with ungolfed method and test cases:

using System;

namespace ROT13
{
    class Program
    {
        static void Main(string[] args)
        {
            Func<string, string> f =
            s =>
            {
                var r = "";
                foreach (var c in s)
                    r += (char)(Char.IsLetter(c) ? (c | 32) < 110 ? c + 13 : c - 13 : c);
                return r;
            };

            Console.WriteLine(f("TOO MANY SECRETS!"));   // GBB ZNAL FRPERGF!
            Console.WriteLine(f("too many secrets!"));   // gbb znal frpergf!
            Console.WriteLine(f("gbb znal frpergf!"));   // too many secrets!
            Console.WriteLine(f("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890,./"));
// NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm1234567890,./
        }
    }
}
\$\endgroup\$
1
\$\begingroup\$

Befunge-93, 72 67 bytes

Edit: -5 bytes thanks to James Holderness

<,<v!:+*"a"*`"`"\`\"{":\*"G"*`"@"\`\"[":::_@#+1:~
+0|_\"N"%6-55*1+%

Try it online!

Kinda disappointed in the previous Befunge answers, so I wrote one with much less whitespace.

\$\endgroup\$
1
\$\begingroup\$

Powershell, 77 bytes

-join($args|% t*y|%{[char]((("$_"-match'[a-m]')-("$_"-match'[n-z]'))*13+$_)})

Test script:

$f = {

-join($args|% t*y|%{[char]((("$_"-match'[a-m]')-("$_"-match'[n-z]'))*13+$_)})

}

@(

,("Hello!","Uryyb!")
,("HELLO","URYYB")
,("URYYB","HELLO")
,("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz","NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm")
,("Why did the chicken cross the road?
Gb trg gb gur bgure fvqr!",
"Jul qvq gur puvpxra pebff gur ebnq?
To get to the other side!")

) | % {
    $s,$expected = $_
    $result = &$f $s
    "$($result-eq$expected): $result"
}

Output:

True: Uryyb!
True: URYYB
True: HELLO
True: NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm
True: Jul qvq gur puvpxra pebff gur ebnq?
To get to the other side!
\$\endgroup\$
1
\$\begingroup\$

Julia 1.0, 69 bytes

(x->(c=Char(x);print(c+isletter(c)*13*(-1)^(x&31>13)))).(read(stdin))

Try it online!

Explanation

Most of the code is an anonymous function that takes an integer codepoint and prints the corresponding ROT-13'd character:

x->(c=Char(x);print(c+isletter(c)*13*(-1)^(x&31>13)))

x->(                                                )  # Anonymous function of x
    c=Char(x);                                         # Cast x to Char and save as c
                    c+                                 # Start with c and add to its codepoint:
                      isletter(c)*                     # If c is not a letter, the rest of this is
                                                       # multiplied by 0
                                  13*(-1)^(       )    # 13, times +/- 1 according to:
                                           x&31>13     # Low 5 bits of x are greater than 13
                                                       # I.e. x > 'M' (if x is uppercase) or x > 'm'
                                                       # (if x is lowercase)
              print(                               )   # Output the resulting character

We then apply this function, vectorized, to the array of (integer) codepoints returned by read(stdin).

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

///, 217 bytes

/|/\/\///a/n|b/o|c/p|d/q|e/r|f/s|g/t|h/u|i/v|j/w|k/x|l/y|m/z|n/a|o/b|p/c|q/d|r/e|s/f|t/g|u/h|v/i|w/j|x/k|y/l|z/m|A/N|B/O|C/P|D/Q|E/R|F/S|G/T|H/U|I/V|J/W|K/X|L/Y|M/Z|N/A|O/B|P/C|Q/D|R/E|S/F|T/G|U/H|V/I|W/J|X/K|Y/L|Z/M/

Because /// have no input, the input is appended to the end of the program. I have seen another answer done so, but forgot where.

Try It Online!

Note that the program is so slow that it takes more than 60s to run and thus will not do the work in the link.

New contributor
TwilightSparkle is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
\$\endgroup\$
  • \$\begingroup\$ Welcome to the site! Would it be possible for you to edit in a link to an online interpreter, such as Try It Online!, so that other users may verify your answer? \$\endgroup\$ – caird coinheringaahing yesterday
  • \$\begingroup\$ @cairdcoinheringaahing thanks! I will. \$\endgroup\$ – TwilightSparkle yesterday
  • \$\begingroup\$ There is as problem: the program is too slow that it takes more than 60s to run. \$\endgroup\$ – TwilightSparkle yesterday
  • \$\begingroup\$ That's fine, just mention that in your post, along with the link \$\endgroup\$ – caird coinheringaahing yesterday
  • 2
    \$\begingroup\$ I dunno, are you sure this isn't jist running into an infinite loop somewhere? Surely it can't be that inefficient that it takes more than 60 seconds to run, even on an empty input \$\endgroup\$ – Jo King yesterday
0
\$\begingroup\$

Scala 108

def m(a:Int,z:Int,c:Int)=if(c>=a&&c<z)a+((c-a+13)%26)else c
"Ok?".map(c=>print(m(65,91,m(97,123,c)).toChar))

ungolfed:

def m (a: Int, z:Int, c:Int) = if (c >= a && c <= z) a+((c-a+13)%26) else c
"Ok?".foreach (c => print (m ('A', 'Z', m ('a', 'z', c)).toChar))

result:

Bx?
\$\endgroup\$
0
\$\begingroup\$

Mathematica 102

StringReplace[Input[],
Join@@Table[Rule@@FromCharacterCode/@{i+c,Mod[i+13,26]+c},{c,{65,97}},{i,0,25}]]
\$\endgroup\$
0
\$\begingroup\$

Haskell (103)

Reused maybe c id $ lookup c from jloy's answer.

main=interact$map(\c->maybe c id$lookup c$[['a'..'z'],['A'..'Z']]>>=zip<*>uncurry(flip(++)).splitAt 13)
\$\endgroup\$
0
\$\begingroup\$

Scala (96 chars)

def r(s:String)=s.map{c⇒val m = if(c<91)65 else 97
if(c.isLetter)
((c+13-m)%26+m).toChar
else c}
\$\endgroup\$
0
\$\begingroup\$

J, 32 29 bytes

a.{~3&u:+13*2|&.>:'@MZ`mz'&I.

Try it online!

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

Jelly, 13 bytes

ØAṙ13żµ;ŒlZyɠ

Try it online!

Still one byte away from 05AB1E ლ(ಠ益ಠლ). Weirdest way I've ever defined a Jelly link, but it works. Takes the string via STDIN (no shorter than taking an ARGV, in this instance)

How it works

ØAṙ13żµ;ŒlZyɠ - Main link. No arguments
ØA            - Yield the upper case alphabet               ["A", "B",   ... "Y", "Z"]
  ṙ13         - Rotate by 13 units left                     ["N", "O",   ... "L", "M"]
     ż        - Zip with alphabet. Call this X              [["N", "A"], ... ["M", "Z"]]
      µ       - Begin a monadic chain with X as argument
        Œl    - X lowercased                                [["n", "a"], ... ["m", "z"]]
       ;      - Concatenate with X                          [["N", "A"], ... ["m", "z"]]
          Z   - Transpose rows and columns                  [["N", ... "m"], ... ["A", ... "z"]]
            ɠ - Yield a line from STDIN. Call this Y        "Hello, World!"
           y  - Transliterate Y based on the mapping in Y   "Uryyb, Jbeyq!"

A lot of the work here is done by the y atom. This is a dyad whose arguments are broken up as follows:

  • Left argument: A two element array, consisting of the following elements:
    • Characters to be changed from.
    • Characters to be changed into.
  • Right argument: Character array to transliterate

This is a good example of how it works.

In the right argument (the character array), it replaces each occurrence of a key in the left argument (the mapping) with the corresponding result. Any characters that aren't a key are just left alone.

One of the key things to grasp is that y doesn't take the mapping in [key, result] pairs. Instead, it takes the list as a transposition of these pairs, so the pairs

[['a', 'b'], ['c', 'd'], ['e', 'f']]

to translate a to b, c to d etc. would be represented as

[['a', 'c', 'e'], ['b', 'd', 'f']]

in order to use y. When using y, if no result is produced and STDERR has an error message, try prepending a Z, to make Zy, and see if that works.

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

Retina, 13 bytes

13+T`zlZL`l_L

Try it online!

The straightforward transliteration that directly applies ROT13 would be 2 bytes longer Try it online!

Explanation

The transliteration stage here is wrapped in a loop stage making it run 13 times (or until the string is not transformed anymore, which can only happen if the input string has no alphabetic characters).

In the transliteration, characters in zlZL are transformed into the characters at the same positions in l_L. Here, z and Z are exactly what they look like, while l and L get converted into the lowercase and uppercase alphabets. The final letters of those alphabets are ignored because they already appear earlier in the string, and _ is just a placeholder to make the uppercase alphabet line up correctly. The result is transforming each letter into the next one (a->b,b->c,...,z->a); doing this 13 times is ROT13.

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

Python 3, 70 bytes

a=input()
for i in a:print(end=i.isalpha()*chr(65+(ord(i)-52)%26)or i)
\$\endgroup\$
  • 2
    \$\begingroup\$ Your proposed solution does not work with lowercase letters. It's not explicitely specified in the challenge though, so I'm not sure if your answer is valid. \$\endgroup\$ – Nathan.Eilisha Shiraini Feb 27 at 18:17
0
\$\begingroup\$

Japt, 28 bytes

c_d)è"%l")?Z|H)<#n?Z+D:Z-D:Z

Try it!

c_d)è"%l")?Z|H)<#n?Z+D:Z-D:Z
c_                                # map input string through a function
  d)è"%l")?                       # regex check if current character is a letter
           Z|H)<#n?               # is it less than the letter n?
                   Z+D:           # if yes, then add 13
                       Z-D:       # otherwise, subtract 13
                           Z      # not a letter, do not transform
\$\endgroup\$
  • \$\begingroup\$ 20 bytes using replace and switching to v2.0a1. There must be a shorter way, though. \$\endgroup\$ – Shaggy Feb 27 at 17:42
  • \$\begingroup\$ 18 bytes \$\endgroup\$ – Shaggy Feb 27 at 17:45
  • \$\begingroup\$ Nice - I had not tried the beta version yet. Looks like regex may have improved? \$\endgroup\$ – dana Feb 27 at 17:47
  • \$\begingroup\$ Yeah, but it's bugged to hell at the moment - only single character class work; anything wrapped in / won't. \$\endgroup\$ – Shaggy Feb 27 at 17:50
  • \$\begingroup\$ 17 bytes. \$\endgroup\$ – Shaggy Feb 27 at 17:50
0
\$\begingroup\$

T-SQL, non-competing (language postdates challenge)

The TRANSLATE function was introduced in SQL Server 2017.

Input is taken from the column I of the table T.

SELECT TRANSLATE(I,A+B,B+A)FROM(VALUES('ABCDEFGHIJKLM','NOPQRSTUVWXYZ'))V(A,B),T

80 bytes

SQL Fiddle

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

PHP - 27 Bytes

<?=str_rot13(fgets(STDIN));
\$\endgroup\$
0
\$\begingroup\$

Zsh, 56 bytes

zsh builtins only. Test this thing on TIO!

Uses decimal-ascii conversion. Perhaps octal would save a few bytes.

for ((;i++<$#1;))printf \\$[[##8](65+(##${1[i]}-52)%26)]

More robust version for 85 bytes: TIO.run

for X in ${(s::)1};{[[ @<$X&&$X<\[ ]]&&printf \\$[[##8](65+(##$X-52)%26)]||printf $X}
\$\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.