Skip to content

Instantly share code, notes, and snippets.

@ivanhjc
Last active October 11, 2015 06:23
Show Gist options
  • Save ivanhjc/0358ce6eb43ef15f853a to your computer and use it in GitHub Desktop.
Save ivanhjc/0358ce6eb43ef15f853a to your computer and use it in GitHub Desktop.
public class C8Ex12CaptainCrunch4 {
public static char convert(String dict, int i, int n) {
if (i+n > 0) {
return dict.charAt((i+n) % 26);
} else {
return dict.charAt(((i+n) % 26 + 26) % 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 = "H Chc Hs ehmzkkx";
System.out.println(encode(s, -51));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment