Skip to content

Instantly share code, notes, and snippets.

@clly
Created April 5, 2012 14:47
Show Gist options
  • Save clly/2311594 to your computer and use it in GitHub Desktop.
Save clly/2311594 to your computer and use it in GitHub Desktop.
Java: Rot13
import java.util.*;
public class CryptRot13 {
public static void main(String args[]) {
Scanner scan = new Scanner(System.in);
String value = "";
String rotValue = "";
CryptRot13 crypt = new CryptRot13();
if(args.length >= 1) {
for(int i = 0; i< args.length;i++) {
value += args[i] + " ";
}
}
else {
System.out.print("Enter a value: ");
value = scan.nextLine();
}
rotValue = crypt.convert(value);
System.out.println("\nOld Value: " + value);
System.out.println("New Value: " + rotValue);
System.out.println("");
}
public String convert(String str) {
StringBuilder val = new StringBuilder();
for(char a:str.toCharArray()) {
if(a >= 'A' && a <= 'Z') {
a += 13;
if(a > 'Z') {
a -= 26;
}
}
else if(a >= 'a' && a <= 'z') {
a += 13;
if(a > 'z') {
a -= 26;
}
}
val.append(a);
}
return val.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment