0

Man I thought I had it! I've been working on the Vigenere problem and have gotten close but keep getting this error when I check. Thoughts?

:) vigenere.c exists

:) vigenere.c compiles

:) encrypts "a" as "a" using "a" as keyword :( encrypts "world, say hello!" as "xoqmd, rby gflkp!" using "baz" as keyword \ expected output, but not "xoqmj, yfz gflkp!\n"

:( encrypts "BaRFoo" as "CaQGon" using "BaZ" as keyword \ expected output, but not "CaQAun\n"

:( encrypts "BARFOO" as "CAQGON" using "BAZ" as keyword \ expected output, but not "CAQAON\n"

:) handles lack of argv[1]

:) handles argc > 2

:) rejects "Hax0r2" as keyword

And my code is:

#include <stdio.h>
#include <cs50.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

#define FALSE 0
#define TRUE 1

int main(int argc, string argv[])
{
    string key = argv[1];
    
    if (argc!= 2)
    {
        printf("please provide only one perameter \n");
        
        // stop the program
        return 1;
    }
    
    // iterate over the key to make sure its all alpha  
    int i,j;
    for (i = 0, j = strlen(key); i < j; i++)
    {
        if (!isalpha(key[i]))
        {
            printf("please use only alphanumeric values \n");
            return 1;
        }
    }
    
    // now we have a key, "key" from the ONE perameter that is all alpha
    
    string message = GetString();
    int k = 0;
    int keyindex;
    
    for (i = 0, j = strlen(message); i < j; i++, k++)
    {
        if (isalpha(message[i]))
        {
            keyindex = k % strlen(argv[1]);
            // covering the upper case letters
            if (isupper(message[i]))
            {
                // covering the upper case letters with upper case key letters

                if isupper(key[i])
                {
                    // print cipher according to two upper case
                    int cipher = ((message[i] - 65 + key[keyindex] - 65) % 26)
                        + 65;
                    printf("%c", cipher);
                }
                else
                {
                    // print according to upper case message lower case key
                    int cipher = ((message[i] - 65 + key[keyindex] - 97) % 26)
                        + 65;
                    printf("%c", cipher);
                }
            }
            // this is for the non upper case letters
            if (islower(message[i]))
            {
                if isupper(key[i])
                {
                    // print cipher according to lower case message and
                    // upper case key letter
                    int cipher = ((message[i] - 97 + key[keyindex] - 65) % 26)
                        + 97;
                    printf("%c", cipher);
                }
                else
                {
                    // print according to lower case message  and lower case key
                    int cipher = ((message[i] - 97 + key[keyindex] - 97) % 26)
                        + 97;
                    printf("%c", cipher);
                }
            }
            
        }
        // non alpha symbols
        else
        {
            printf("%c", message[i]);
        }
    }
    
    // end program after iterating
    printf("\n");

}
0

In previous years' problem sets, key was case-insensitive, so "A" is the same as "a". I don't know if this year's problem set states otherwise, but you are taking case of key into account. Check the specs again if this should indeed be the case.

2
  • Hey @ronga! So the answers might be case in-sensitive? – Ben Poretzky Apr 14 '16 at 7:49
  • No, in previous psets, the key was case-insensitive but the plaintext was case-sensitive. Check the current pset specs if that is still the case. – ronga Apr 15 '16 at 1:49
0

You aren't checking for case for the "keyindex" only the character you're indexed into in the message.

What I found useful was doing the math for the key in the actual "keyindex" variable declaration. You can try an ifupper() or islower() approach or you can try changing the case in the declaration and subtracting the appropriate ascii amount for whatever case you change it to in the declaration.

You must log in to answer this question.

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