0

After many hours of tinkering and research, I still don't grasp why my vigenere code fails check50 on these two accounts:

1) encrypts "world, say hello!" as "xoqmd, rby gflkp!" using "baz" as keyword

2) encrypts "BaRFoo" as "CaQGon" using "BaZ" as keyword

https://sandbox.cs50.net/checks/f5d3393fa6a5413cb1aa4c5b3965beeb

My code:

if (argc != 2)
{
    printf("Please enter a single key word at the command line.\n");
    return 1;
}

else if (argc == 2)
{

    for (int j = 0, n = strlen(argv[1]); j < n; j++)
    {    
        int flag = 0;

        if (isalpha(argv[1][j]))
        { 
           flag = 0;   
        }

        else
        {
            flag = 1;
            printf("Please enter only alphabetic characters.\n");
            return 1;
        }
    }

    // printf("Please enter some plain text to encrypt.\n");

    string p = GetString();
    string k = argv[1];

    for (int i = 0, pl = strlen(p), kl = strlen(k), j = 0; i < pl; i++)
    {
        char curr = p[i];

        if (isalpha(curr) && isupper(curr))
        {
            int c = i % kl;
            int key_char = k[c] - 65;
            int alpha = p[i] - 65;
            int alpha_adj = (alpha + key_char) % 26;
            int ascii = alpha_adj + 65;

            printf("%c", ascii);
            j = (j + 1) % kl;
        }

        else if (isalpha(curr) && islower(curr))
        {
            int c = i % kl;
            int key_char = k[c] - 97;
            int alpha = p[i] - 97;
            int alpha_adj = (alpha + key_char) % 26;
            int ascii = alpha_adj + 97;

            printf("%c", ascii);
            j = (j + 1) % kl;
        }

        else
        {
            printf("%c", curr);
        }
    }
    printf("\n");    
    return 0;
}

Up-voted responses to other similar questions here on cs50.stackexchange have suggested that including printf() prompts that are not explicitly required in the specs will cause certain check50 failures.

However, I've tried removing those prompts in my code, but each time I do it just results in other check50 failures that seemingly have no logical connection to them. In general, I'm unable to discern from sandbox why these failures are occurring and what corrective action to take. I'm fresh out of ideas.

3

Without digging deep into your code, I see two significant problems. They also happen to be the two most common errors on this pset. First, the code uses the same index, i, for both the plain text and the key. (It looks like you started to, with j, but didn't finish implementing it.) The problem with this is that while the plaintext will process every character, the encoding process will only use a character from the key when a letter is actually encoded. In plain terms, when you use the same index and a non-alpha is processed from the plain text, the key will get out of synch with the text letters and you will use the wrong key to encode.

Next, the code does nothing to handle the case where the plain text letter and the key letter are not the same case. Your code only works if both are either uppercase or lowercase.

And a tip: if something is isupper() or islower(), it implies that it is also isalpha(). Testing for both is unnecessary.

There may be other issues, but this will get you going again.

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

1
  • Thank you. Correcting those two errors resulted in a successful pass through check50. I'd created "int j" in my for loop but forgot to implement it through k[j]. And I'd also neglected to check for all possible variations of upper/lower case between plaintext and the key.
    – MEK
    Mar 15 '16 at 17:18

You must log in to answer this question.

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