Caesar cipher encoding in C++: chars won't loop back to 'a' or 'A' -
i'm working on coding ceasar cipher reads plaintext .txt, encrypts plaintext , writes second .txt, reads second .txt , decrypts third .txt. working except encryption of characters near end of alphabet. when character reaches 'z' or 'z' should loop 'a' or 'a'. below snippet of code encoding function, bit that's causing issues.
if (isalpha(inputstring[i])) { //use isalpha() ignore other characters (int k = 0; k < key; k++) { //key calculated in function, 6 in case if (inputstring[i] == 'z') //these statements don't seem work encryptedstring[i] = 'a'; else if (inputstring[i] == 'z') encryptedstring[i] = 'a'; else //this part works correctly encryptedstring[i] ++; } }
input:
the quick brown fox
jumped on the----
house or moon or some-thing.
expected output:
znk waoiq hxuct lud
pasvkj ubkx znk----
nuayk ux suut ux yusk-znotm.
actual output:
the q{ick bro}n fo~
j{mped o|er the----
ho{se or moon or some-thing.
key: 6
ciphered[i] = (input[i] + key) % ('z' - 'a') + 'a'
– zivs apr 7 @ 17:24+'a'
. otherwise won't valid ascii. – jonas apr 7 @ 17:25