Search code examples
javaencryptionrot13

Rot 13 Minimal Version Java


I tried to implement Rot13 and to make it as minimal as possible, this are my results so far:

    if ( (c >= 'A') && (c <= 'Z') ) 
        c=((c-'A'+13)%26)+'A';

    if ( (c >= 'a') && (c <= 'z') )
        c=((c-'a'+13)%26)+'a';

    return c;

I showed this to my Prof and he said it would be possible in two lines. I don't know how i could shrink this code further and not generating wrong output.

Thanks for your help

EDIT: if nothing changed (outer range ascii) it should only return c. Maybe the solution is the second answer + return line c in case nothing returned.


Solution

  • Slightly more correct than Sibbo's answer. This returns c as is if it falls in neither range. and in 1 line.

    return ((c >= 'A') && (c <= 'Z')) ? ((c-'A'+13)%26)+'A'
                                     :((c >= 'a') && (c <= 'z') ? ((c-'a'+13)%26)+'a'
                                                                      : c);