Hello,
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:
Is there a way I can make 9 wrap around to A such that instead of the traditional?
so that instead of:
ABCDEFGHIJKLMNO PQRSTUVXYZ (Z wraps to A)
what i'm trying to do is:
ABCDEFGHIJKLMNO PQRSTUVXYZ01234 65789 (9 wraps to A)
Thanks in advance for any help!
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:
Code:
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:
ABCDEFGHIJKLMNO PQRSTUVXYZ (Z wraps to A)
what i'm trying to do is:
ABCDEFGHIJKLMNO PQRSTUVXYZ01234 65789 (9 wraps to A)
Thanks in advance for any help!
Comment