The alphabets in English are used in C as defined in ASCII. 'Z' (ASCII 90) is followed by '{' only (ASCII 91). To go back to 'A', you should do all the shifts in the following manner:
- Subtract your ASCII character by 65. It'll lead to output between 0 to 25 (inclusive).
- Add the displacement (shift distance).
- Take a modulo 26, to wrap around your result.
- Add 65 back again.
Remember, this will work only for capital alphabets of the English language. So you may want to use toupper()
from the ctype.h
library.
If you want to add similar functionality for small characters, do the above procedure, replacing 65 by 97.
To check if you got a small character or a capital, use isupper()
.
You'll have to add more and specific code for special characters.