This kind of question has been asked before, but I wanted to make it more specific. The linked question asks to read from stdin and rot13 the input. I don't want the disadvantage of I/O code that differs a lot for each language in length.

The challenge:

Write a function that will ROT13 (wiki link) one char (as defined by the ASCII table).
No built-in ROT13 functionality allowed.

Shortest code wins.

You can use this kind of C-family loop to test your function:

for (int i = '!'; i <= '~'; ++i)
    char c = (char) i;
    System.out.println(c + "   ->   " + f(c));
  • \$\begingroup\$ @Howard: I linked that question myself. But that is different. Here the task is to rotate only one char. \$\endgroup\$ Jan 2, 2014 at 17:49
  • \$\begingroup\$ The difference is minor only - almost all answers from the other question can be user with litte modifications. \$\endgroup\$
    – Howard
    Jan 2, 2014 at 17:50
  • \$\begingroup\$ @Howard: The most important difference is that I don't want the I/O code here. Just the plain algorithm. \$\endgroup\$ Jan 2, 2014 at 17:52
  • 2
    \$\begingroup\$ In my opinion changing I/O doesn't justify a new question in this case. \$\endgroup\$
    – Howard
    Jan 2, 2014 at 17:55

2 Answers 2


C (77 64 chars)

char f(char c){return(c>64&&c<91)?65+(c-52)%26:(c>96&&c<123)?97+(c-84)%26:c;}

char f(char c){return(isalpha(c))?65+(c&32)+(c-52-(c&32))%26:c;}

I came up with this in Java (81 chars):

char f(char c){return Character.isLetter(c)?(char)((((c&95)-52)%26+65)|c&32):c;}

Demo here: http://ideone.com/ItT7IM


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