I'm trying to code a simple Caesar Cipher in C. I'm creating an encrypt function that receives a string (char *, the text to be encrypted) and an integer (the key).
In the function, I allocate memory for an empty string that will receive the shifted chars. Then, I iterate through every char in the initial string and ask if it is a letter char (A-z) or not. If it is, it shiftes according to key. If it is not, it simply repeats the current char. The problem is: when chars such as !, ?, . or even space appear at the end, it adds some '?' to it. I have put printf statements and my guess is that undefined behavior is occuring, but I can't figure it out myself. I hope someone can help me. Below, it is the code I wrote and the strange results.
char* encrypt(char* entry, int key) {
int i = 0;
key = key % 26;
char * tmp = (char *)malloc(strlen(entry));
if (!tmp) {
printf("Error during allocation.\n");
return entry;
}
//memset(tmp, 0, 1); // Tried with and without it.
char t;
while ((t = *(entry + i))) {
printf("Current letter: %c\n",*(entry+i));
if ((t >= 65 && t <= 90) || (t >= 97 && t <= 122)) { //is letter
*(tmp + i) = t + key > 90 ? t + key - 26 : t + key;
}
else { //isnt letter
printf("No letter char appeared. Code = %d\n",t);
*(tmp+i) = t;
}
printf("tmp letter: %c\n",*(tmp+i));
printf("current tmp: %s\n----------------\n",tmp);
i++;
}
printf("final tmp = %s\n",tmp);
entry = tmp;
free(tmp);
return entry;
}
Calling the function: encrypt("HELLO! HOW ARE YOU?!", 13);
Expected (final) result: URYYB! UBJ NER LBH?! Actually (final) result: URYYB! UBJ NER LBH?!? (sometimes it adds more '?')
Debugging printf statements:
Current letter: H
tmp letter: U
current tmp: U
----------------
Current letter: E
tmp letter: R
current tmp: UR
----------------
...
----------------
Current letter: !
No letter char appeared. Code = 33
tmp letter: !
current tmp: URYYB!
----------------
Current letter:
No letter char appeared. Code = 32
tmp letter:
current tmp: URYYB! ? // <<< It added a strange character to the string
----------------
Current letter: H
tmp letter: U
current tmp: URYYB! U // <<< '?' strange character gone
----------------
Current letter: O
tmp letter: B
current tmp: URYYB! UB
----------------
Current letter: W
tmp letter: J
current tmp: URYYB! UBJ // (I)
----------------
Current letter:
No letter char appeared. Code = 32
tmp letter:
current tmp: URYYB! UBJ // This time, space didn't raise a strange char after (I)
----------------
...
----------------
Current letter:
No letter char appeared. Code = 32
tmp letter:
current tmp: URYYB! UBJ NER ? // Missed me? I'm back
----------------
Current letter: Y
tmp letter: L
current tmp: URYYB! UBJ NER L // ...And gone again
----------------
Current letter: O
tmp letter: B
current tmp: URYYB! UBJ NER LB
----------------
Current letter: U
tmp letter: H
current tmp: URYYB! UBJ NER LBH???
----------------
Current letter: ?
No letter char appeared. Code = 63
tmp letter: ?
current tmp: URYYB! UBJ NER LBH???
----------------
Current letter: !
No letter char appeared. Code = 33
tmp letter: !
current tmp: URYYB! UBJ NER LBH?!?
----------------
final tmp = URYYB! UBJ NER LBH?!?
Does anyone have an explanation to this happening? GCC info on my sys:
gcc -v
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 9.1.0 (clang-902.0.39.1)
Target: x86_64-apple-darwin17.5.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin