Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static final int A_CHAR = (int) 'a';
- public static final int Z_TO_A = (int) ('z' - 'a') + 1;
- // alphabet frequency
- static double[] table = { 8.2, 1.5, 2.8, 4.3, 12.7, 2.2, 2.0, 6.1, 7.0, 0.2,
- 0.8, 4.0, 2.4, 6.7, 7.5, 1.9, 0.1, 6.0, 6.3, 9.1, 2.8, 1.0, 2.4, 0.2,
- 2.0, 0.1 };
- // convert letter to number
- static int let2nat(char c) {
- return ((int) c) - A_CHAR;
- }
- // convert number to letter
- static char nat2let(int code) {
- return (char) (code + A_CHAR);
- }
- // shift a letter to another letter shftAmt spaces away
- static char shift(int shftAmt, char c) {
- if (let2nat(c) < 0 || let2nat(c) > Z_TO_A - 1) {
- return c;
- } else {
- int result = (let2nat(c) + shftAmt) % Z_TO_A;
- result += Z_TO_A;
- result %= Z_TO_A;
- return nat2let(result);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement