ROT-13 function in java?

Is there already a rot13() and unrot13() implementation as part of one of the standard Java libraries? Or do I have to write it myself and "reinvent the wheel"?

It might look something like this:

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

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

  return c; 
}

Solution 1:

Might as well contribute my function to save other developers the valuable seconds

public static String rot13(String input) {
   StringBuilder sb = new StringBuilder();
   for (int i = 0; i < input.length(); i++) {
       char c = input.charAt(i);
       if       (c >= 'a' && c <= 'm') c += 13;
       else if  (c >= 'A' && c <= 'M') c += 13;
       else if  (c >= 'n' && c <= 'z') c -= 13;
       else if  (c >= 'N' && c <= 'Z') c -= 13;
       sb.append(c);
   }
   return sb.toString();
}

Solution 2:

I don't think it's part of Java by default, but here's an example of how you can implement it;

public class Rot13 { 

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

}

Source: http://introcs.cs.princeton.edu/java/31datatype/Rot13.java.html