Skip to content

Instantly share code, notes, and snippets.

@ivanhjc
Created October 12, 2015 10:33
Show Gist options
  • Save ivanhjc/ac45a473fc436ce107ce to your computer and use it in GitHub Desktop.
Save ivanhjc/ac45a473fc436ce107ce to your computer and use it in GitHub Desktop.
public class C8Ex12CaptainCrunch3 {
public static char convert(String dict, int i, int n) {
if (n >= 0) {
if (i+n < 26) {
return dict.charAt(i+n);
} else {
return dict.charAt(i+n-26);
}
} else {
if (i+n > 0) {
return dict.charAt(i+n);
} else {
return dict.charAt(i+n+26);
}
}
}
public static String encode(String s, int n) {
String d = "abcdefghijklmnopqrstuvwxyz";
String D = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int i = 0;
String box = "";
while (i < s.length()) {
if (s.charAt(i) == ' ') {
box = box + ' ';
} else {
int j = d.indexOf(s.charAt(i));
if (j != -1) {
box = box + convert(d, j, n);
} else {
j = D.indexOf(s.charAt(i));
box = box + convert(D, j, n);
}
}
i = i + 1;
}
return box;
}
public static void main(String[] args) {
String s = "I dont want to tell you how to dO it";
System.out.println(encode(s, 15));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment