# ROT-13 transform standard 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.

• The problem shouldn't be a tag, so I removed ROT13, just an FYI – Nick Berardi Jan 27 '11 at 21:18
• Don't you mean A-Za-z (to count both upper- and lower-case) ? – Joey Adams Jan 27 '11 at 21:33
• @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. – Nakilon Jan 30 '11 at 18:59
• Saying xor is not encryption is like saying a+b is not math. – Nakilon Jan 30 '11 at 19:02
• 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. – Angs Dec 3 '16 at 10:33

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

• Since nobody said it in >2 years, welcome to PPCG! – Erik the Outgolfer Oct 6 '16 at 11:43

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


# 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)}))

• Invalid because the interpreter is not available. – cat Feb 22 '16 at 21:29
• @cat Somehow I got the url wrong. It is fixed now. – Ian D. Scott Feb 23 '16 at 23:24

## 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])


# 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


# 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,./
}
}
}


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

# 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 =$_

main=interact$map(\c->maybe c id$lookup c$[['a'..'z'],['A'..'Z']]>>=zip<*>uncurry(flip(++)).splitAt 13)  # 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}  # J, 32 29 bytes a.{~3&u:+13*2|&.>:'@MZmz'&I.  Try it online! # 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. # Retina, 13 bytes 13+TzlZLl_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. # Python 3, 70 bytes a=input() for i in a:print(end=i.isalpha()*chr(65+(ord(i)-52)%26)or i)  • 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. – Nathan.Eilisha Shiraini Feb 27 at 18:17 # 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  • 20 bytes using replace and switching to v2.0a1. There must be a shorter way, though. – Shaggy Feb 27 at 17:42 • 18 bytes – Shaggy Feb 27 at 17:45 • Nice - I had not tried the beta version yet. Looks like regex may have improved? – dana Feb 27 at 17:47 • Yeah, but it's bugged to hell at the moment - only single character class work; anything wrapped in / won't. – Shaggy Feb 27 at 17:50 • – Shaggy Feb 27 at 17:50 # PHP - 27 Bytes <?=str_rot13(fgets(STDIN));  # Zsh, 126112 106 bytes Shell builtins only. Rotates any letter in A-Za-z, using decimal-ascii conversion. 126bytes 112bytes 106 bytes r()printf \\$[[##8](13+$1+(##$X-$1)%26)] for X (${(s::)1}){case $X {[A-Z])r 52;;[a-z])r 84;;*)printf$X;}}


Initial (wrong) solutions: 56bytes 83bytes

# T-SQL, 80 bytes

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


SQL Fiddle