Rot13 in Java

Published on 28 April 2022 (Updated: 25 September 2022)

Rot13 in Java

Welcome to the Rot13 in Java page! Here, you'll find the source code for this program as well as a description of how the program works.

Current Solution

public class Rot13
{
   public static void main(String[] args)
   {
      if(args.length<1) {
         System.out.println("Usage: please provide a string to encrypt");
      }
      else {
         String code = args[0];
         String result = "";
         if(code.length()==0) {
            System.out.println("Usage: please provide a string to encrypt");
         }
         else {
            String lower = "abcdefghijklmnopqrstuvwxyz";
            String upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            for(int a=0; a<code.length(); a++) {
               String ch = code.substring(a,a+1);
               int l = lower.indexOf(ch);
               int u = upper.indexOf(ch);
               if(l!=-1) {
                  result+=lower.substring( (l+13)%26,(l+14)%26!=0 ? (l+14)%26 : l+14 );
               }
               else if(u!=-1) {
                  result+=upper.substring( (u+13)%26,(u+14)%26!=0 ? (u+14)%26 : u+14 );
               }
               else {
                  result+=ch;
               }
            }
            System.out.println(result);
         }
      }
   }
}

Rot13 in Java was written by:

If you see anything you'd like to change or update, please consider contributing.

Note: The solution shown above is the current solution in the Sample Programs repository as of Oct 11 2020 18:07:28. The solution was first committed on Oct 27 2019 22:01:29. As a result, documentation below may be outdated.

How to Implement the Solution

No 'How to Implement the Solution' section available. Please consider contributing.

How to Run the Solution

No 'How to Run the Solution' section available. Please consider contributing.