Advertisement
Guest User

mod with negative results

a guest
Jul 8th, 2015
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. public static final int A_CHAR = (int) 'a';
  2. public static final int Z_TO_A = (int) ('z' - 'a') + 1;
  3. // alphabet frequency
  4. static double[] table = { 8.2, 1.5, 2.8, 4.3, 12.7, 2.2, 2.0, 6.1, 7.0, 0.2,
  5. 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,
  6. 2.0, 0.1 };
  7.  
  8. // convert letter to number
  9. static int let2nat(char c) {
  10. return ((int) c) - A_CHAR;
  11. }
  12.  
  13. // convert number to letter
  14. static char nat2let(int code) {
  15. return (char) (code + A_CHAR);
  16. }
  17.  
  18. // shift a letter to another letter shftAmt spaces away
  19. static char shift(int shftAmt, char c) {
  20. if (let2nat(c) < 0 || let2nat(c) > Z_TO_A - 1) {
  21. return c;
  22. } else {
  23. int result = (let2nat(c) + shftAmt) % Z_TO_A;
  24. result += Z_TO_A;
  25. result %= Z_TO_A;
  26. return nat2let(result);
  27. }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement