I'm trying to make a simple Caesar cipher however instead of A-Z i want to also include 0-9. where 9 wraps back to A.
The best i could can do shift letters and integers independently so a string like "TESTING SHIFT Z 9" with a key of 1
becomes
"UFTUJOH TIJGU A 0"
9 wraps to 0 and Z wraps to A
here is how I did that:
Expand|Select|Wrap|Line Numbers
- private char encryptChar(char c, int k) {
- if (Character.isDigit(c))
- return (char) ('0' + (c - '0' + k) % 10); //handles the ints
- else if (Character.isLetter(c))
- return (char) ('A' + (c - 'A' + k) % 26); //handles the letters
- else
- return c;
- }
so that instead of:
ABCDEFGHIJKLMNOPQRSTUVXYZ (Z wraps to A)
what i'm trying to do is:
ABCDEFGHIJKLMNOPQRSTUVXYZ0123465789 (9 wraps to A)
Thanks in advance for any help!