ROT-13 function in java?

31.3k views Asked by At

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; 
}
6

There are 6 answers

0
Chris Thompson On BEST ANSWER

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

0
AudioBubble On
private final static List<String> upperAlphabets = Arrays.asList(
        "A", "B", "C", "D", "E", "F", "G",
        "H", "I", "J", "K", "L", "M", "N",
        "O", "P", "Q", "R", "S", "T", "U",
        "V", "W", "X", "Y", "Z");
private final static List<String> lowerAlphabets = Arrays.asList(
        "a", "b", "c", "d", "e", "f", "g",
        "h", "i", "j", "k", "l", "m", "n",
        "o", "p", "q", "r", "s", "t", "u",
        "v", "w", "x", "y", "z");

 private static void rot(int toSkip, String value) {
        StringBuilder sb = new StringBuilder();
        int pos = 0, newPos = 0;
        boolean upper;
        for (char c : value.toCharArray()) {
            pos = upperAlphabets.indexOf(String.valueOf(c));
            if (pos == -1) {
                pos = lowerAlphabets.indexOf(String.valueOf(c));
                upper = false;
            } else {
                upper = true;
            }

            if (pos + toSkip > 25) {
                newPos = (pos + toSkip) % 26;
            } else {
                newPos = pos + toSkip;
            }

            if (upper) {
                sb.append(upperAlphabets.get(newPos));
            } else {
                sb.append(lowerAlphabets.get(newPos));
            }
        }
        System.out.println(sb);
    }

This is not only about rot13, this can do rot100 or rot1213 anything depending upon what value you are passing and the most important thing is that both list of upper case and lower case alphabets are must.

1
Mr Good On

you be doing like dis:

pblic class Rot13 {

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

Sometimes inputs might have special characters, so it's better to include them.

    String input = "Do you have any cat pictures?";
    StringBuilder op = new StringBuilder();

    for (int i = 0; i < input.length(); i++) {
        char c = input.charAt(i);

        if (c == ' ') {
            op.append(" ");
        } else if (c >= 'a' && c <= 'z') {
            if (c > 'm') c -= 13;
            else c += 13;
            op.append(c);
        } else if (c >= 'A' && c <= 'Z') {
            if (c > 'M') c -= 13;
            else c += 13;
            op.append(c);
        } else {
            op.append(c);
        }
    }
3
georgiecasey On

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();
}
0
Rishabh Gupta On
public static String rot13(String message) {
     String a  = "";
     
     for(int i =0; i<message.length(); i++){
       if(message.charAt(i)>=65 && message.charAt(i)<=77 ||
          message.charAt(i)>=97 && message.charAt(i)<=109 ){
         a +=  (char)(message.charAt(i)+13);
         
       }
      
       else if(message.charAt(i)>=78 && message.charAt(i)<=90||
          message.charAt(i)>=110 && message.charAt(i)<=122 ){
         a +=  (char)(message.charAt(i)-13);
         
       }
         else {
           a +=  (char)(message.charAt(i));
         }
       
       
       
     }
     
     return a;
   }