0
// ENCIPHER:
// assign argv[1] to a string called 'code'
string key = argv[1];

// For each letter in the plaintext, execute a shift in the letter printed.
for (int letter_index = 0, key_index = 0, word_length = strlen(plain_text), key_length = strlen(key); letter_index < word_length; letter_index++)
{
    // Only cipher alpha characters.
    if (isalpha(plain_text[letter_index]))
    {
        // Preserve lowercase.
        if (islower(plain_text[letter_index]) && islower(key[key_index]))
        {
            printf("%c", (((plain_text[letter_index] - 'a') + (key[key_index % key_length] - 'a')) % 26 + 'a'));
            key_index++;
        }

        if (islower(plain_text[letter_index]) && isupper(key[key_index]))
        {
            printf("%c", (((plain_text[letter_index] - 'a') + (key[key_index % key_length] - 'A')) % 26 + 'a'));
            key_index++;
        }

        // Preserve uppercase.
        if (isupper(plain_text[letter_index]) && islower(key[key_index]))
        {
            printf("%c", (((plain_text[letter_index] - 'A') + (key[key_index % key_length] - 'a')) % 26 + 'A'));
            key_index++;
        }

        if (isupper(plain_text[letter_index]) && isupper(key[key_index]))
        {
            printf("%c", (((plain_text[letter_index] - 'A') + (key[key_index % key_length] - 'A')) % 26 + 'A'));
            key_index++;
        }
    }

    // If non-alpha characters, print as usual.
    else
    {
        printf("%c", plain_text[letter_index]);
    }
}

I am trying to figure out what part of the code above is incorrect;

When I implement ./vigenere with a key of 'bacon,' and encrypt the phrase "Meet me at the park at eleven am", the output is only "Negh z."

I believe the code stops encrypting the phrase once it reaches the end of the key word. I previously had written it without the && to check the case of the keyword and it executed correctly, but only with lowercase keywords.

Why does the code above end the cycling of the keyword?

1

Without actually executing the code, my educated guess is that it doesn't actually stop working, but is encoding the remaining plaintext as non-printable characters. This happens because the key_index will just keep incrementing and use whatever is stored in memory following the actual key. There's nothing in c that prevents the code from going beyond the end of the key var (unless it goes beyond the memory space allocated to the entire program).

There's something missing from the code. Once key_index exceeds the length of the key, meaning that all of the key chars have been used, it needs to be reset back to 0, so that the key is used over (and over and over).

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

| improve this answer | |
  • I thought I had done that by modding the key_index by key_length in the equation, as in "key[key_index % key_length]" – Rachel N. Aug 23 '18 at 15:56
  • I went through debug50 and realized that I needed to include modding the key_index by key_length in the boolean expression as well! For example, instead of if (isupper(plain_text[letter_index]) && islower(key[key_index])), I needed if (isupper(plain_text[letter_index]) && islower(key[key_index % key_length])). Otherwise, the loop would not execute once the key had reached the end. – Rachel N. Aug 23 '18 at 16:14

You must log in to answer this question.

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