There are a number of correctness and stylistic issues with the code.
Correctness issues
The loop variable, m_inp
, is not initialized.
It is unlikely that an unsigned char
can ever equal EOF
. The value of EOF
is -1. The loop will never finish (unless you are on a very strange architecture).
The traditional Caesar cipher operates alphabetically, rather than on the whole ASCII character set.
Stylistic issues
Using more "stylistically correct" C will make it easier for other programmers to read your code. These are subjective.
The name m_inp
is a poor name for a loop variable in a short loop. Use i
instead.
It is easier to read ptr[i]
instead of *(ptr + i)
, even though they are exactly equivalent in C.
Marking the input as const unsigned char *
makes it more obvious that it is not modified.
Use int
instead of char
for the offset.
Traditionally, function outputs go on the left, inputs go on the right. This is not important, however.
Revised code
Here is a sample revision. It takes a NUL-terminated string, and writes the Caesar encoded / decoded version to an output buffer. This is how I would write the function, not necessarily how you would write it:
void caesar(char *output, const char *input, int shift)
{
int i;
for (i = 0; input[i]; i++) {
if (input[i] >= 'a' && input[i] <= 'z')
output[i] = (input[i] - 'a' + shift) % 26 + 'a';
else if (input[i] >= 'A' && input[i] <= 'Z')
output[i] = (input[i] - 'A' + shift) % 26 + 'A';
else
output[i] = input[i];
}
output[i] = '\0';
}
Note that Caesar encryption and decryption are actually the same operation, just with different shift values. So if you encrypt with a shift of 3, you can decrypt with a shift of -3 (or equivalently, 23).
Also note that you can replace unsigned char
with char
or vice versa and everything will work exactly the same (although you'd need to cast the inputs).