for (int i = 0, j = strlen(s), k = 0; i < j; i++, k++)
{
if (isalpha(s[i]))
{
if (isupper(s[i]))
{
if (argv[1][k] != '\0' && argv[1][k] != ' ')
{
printf("%c", (((s[i] - 65) + (toupper(argv[1][k]) - 65) % 26) + 65));
}
else
{
k = 0;
printf("%c", (((s[i] - 65) + (toupper(argv[1][k]) - 65) % 26) + 65));
}
}
if (islower(s[i]))
{
if (argv[1][k] != '\0' && argv[1][k] != ' ')
{
printf("%c", (((s[i] - 97) + (toupper(argv[1][k]) - 65) % 26) + 97));
}
else
{
k = 0;
printf("%c", (((s[i] - 97) + (toupper(argv[1][k]) - 65) % 26) + 97));
}
}
}
else
{
printf("%c", s[i]);
k--;
}
}
I've a got a little problem with this, I tried running the debugger with several breakpoints. And, I've found that the problem occurs when my k = 3
. I've gone through it step-by-step and don't see why it would skip over the printf function.
So if I had a key of "bacon", and plaintext of "Meet me at the park at eleven am".
I would get the ciphertext of: "Neg zf av uf pcx bt gzrwep oz".
Whereas the correct would be: "Negh zf av huf pcfx bt gzrwep oz".
So, when my k = 3
(the o), my i = 3
(the t). "t" is a lower case, and "o" is neither '\0' nor a space, so the printf function should run. Instead it does nothing and goes back to the start of the for loop, making k = 4 and i = 4.