I created this Caesar Cipher program, but for some reason, I can't find its bug. Here is the code. It compiles, but it just prints out the same thing I enter, for any key.
#include <cs50.h>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <ctype.h>
string encryptor(string encryptString, int key);
int main(int argc, string argv[]) {
if (argc >= 2) {
int k = atoi(argv[1]);
string stringToBeEncrypted = GetString();
printf("%s\n", encryptor(stringToBeEncrypted, k));
} else {
printf("Please add a key argument\n");
return 1;
}
}
string encryptor(string encryptString, int key) {
for (int i = 0, n = strlen(encryptString); i < n; i++) {
int ascii = (int) encryptString[i];
key = key % 26;
if (isalpha(encryptString[i]) == true) {
if (islower(encryptString[i]) == true) {
ascii = ((ascii-96+key) % 26) + 96;
}else {
ascii = ((ascii-64+key) % 26) + 64;
}
// ascii += key;
// if (ascii > 122) {
// //ascii = ascii - 122 + 96;
// ascii = (ascii % 26) + 97;
// } else if (ascii > 90 && ascii < 97) {
// //ascii = ascii - 90 + 64;
// ascii = (ascii % 26) + 65;
// }
}
char newChar = (char) ascii;
encryptString[i] = newChar;
}
return encryptString;
}