0
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.

1

It's not skipping an if. The printf function does run. However, it's "printing" an "unprintable" character.

Check your equations. When i,k = 3

(((s[i] - 97) + (toupper(argv[1][k]) - 65) % 26) + 97); =>

(((t - 97) + ('O' - 65) % 26) + 97) =>

(((19) + (14) % 26 ) + 97) =>

Get rid of the outer parens (personal choice, it's easier to "read")

((19) + (14) % 26) + 97 =>

By operator precedence, it will do (14) % 26 first =>

((19) + 14) + 97 => 130 => unprintable (ascii range is 0 - 127)

If you want to "see" what the computer "sees" you can run ./vigenere bacon | cat -v. This will pipe the output to cat -v; the v switch is "show nonprinting".

  • Wow can't believe I overlooked something as simple as the order of operations... Thanks so much! – Knovolt Jun 14 '17 at 4:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .