// 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?