1

I am trying to complete vigenere's cipher by the end of tonight before I am shortly without internet. I have been working on this one error for several hours and cannot figure out how to fix it. The Check50 expects "CaQGon" when BaRFoo is the keyword, but my code is returning "CaQGoh\n". My code holds up for all other cases. I am just having issues when I use a code with a mix of capitals and lowercases. I would appreciate help so very much! Thank you!

{
string keyword = argv[1];
if (argc != 2)
{
    printf("Please enter a valid keyword.\n");
    return 1;
}
else
{
    for (int i = 0, k = strlen(keyword); i < k; i++)
    {        
        if (!isalpha(keyword[i]))
        {
            printf ("Input a valid keyword. \n");
            return 1;
        }
    }
    string message = GetString();
    int k = 0;
    for (int j = 0; j < strlen(message); j++)
    {
        if (isalpha(message[j]))
            {
            if (isupper(message[j]))
            {
                message[j] = (message[j] -'A' + (keyword[k%(strlen(keyword))]) - 'A') % 26 + 'A';
                k++;
            }
            else if (islower(message[j]))
            {
                message[j] = (message[j] -'a' + (keyword[k%(strlen(keyword))]) - 'a') % 26 + 'a';
                k++;
            }
        }
        else
        {
            message[j] = message[j];
        }
    }
    printf ("%s\n", message);
    return 0;
}

}

1
  • I see I was wrong.
    – ebobtron
    Jul 31 '15 at 21:37
3

This is a common mistake. Your code requires that the plain text to be encoded and the key are both the same case. You haven't allowed for the scenario where one is uppercase and the other is lowercase or vice versa.

If this answers your question, please accept this answer to remove the question from the unanswered question pool. Let's keep up on forum housekeeping. ;-)

4
  • I understand that that is my issue, but have been struggling for a while with finding a way to correct this gap in my code. Do I need to add 2 more if and else if statements within my isupper and islower? Or can I simply alter my ASCII math equations?
    – jillbas
    Jul 31 '15 at 21:44
  • Your first idea is a popular one and often used. The second idea is a possibility, but it would depend on what you would do. A third idea is to convert each element in key from it's ASCII value to "something else" (that's a hint) before starting the encoding process.
    – Cliff B
    Jul 31 '15 at 21:50
  • Got it! Thank you so much! I don't know if my solution was the best way to do it, but it worked. I made all of the chars in my keyword to lowercase and then noticed that my capitalized letters were off by 6 so I just made that adjustment to the ascii math and got all green! Thank you again!
    – jillbas
    Jul 31 '15 at 22:24
  • that worked. Or, you could have just converted all the key letters to numbers between 0 and 25 includsive and simplified the math. ;-)
    – Cliff B
    Jul 31 '15 at 22:31

You must log in to answer this question.

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