20

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; 
}
james.garriss
  • 12,959
  • 7
  • 83
  • 96
Regex Rookie
  • 10,432
  • 15
  • 54
  • 88

5 Answers5

29

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();
}
georgiecasey
  • 21,793
  • 11
  • 65
  • 74
21

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

RobinJ
  • 5,022
  • 7
  • 32
  • 61
Chris Thompson
  • 35,167
  • 12
  • 80
  • 109
1
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.

0

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);
        }
    }
malware_656
  • 135
  • 5
  • 8
-1

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();
}
  • Why copy the accepted message and make it worse by breaking the formatting and deleting some characters? What's the point of that? – Generous Badger Aug 26 '21 at 08:39