0

I've gone through the code for almost half a day into midnight and have tried many ways, but still do not know where is wrong. Other checks passed, it cannot correctly cipher texts according to the key, but the formula works in caesar.c. Can anyone give me some help? Great many thanks!

int main(int argc, string argv[])
{
    if (argc != 2)
    {   
        //check if it has one single command-line arguement
        printf("usage:./vigenere <keyword>\n");
        return 1;
    } else 
    {   
        //check if the key is all made up by alphabetical characters
        for (int i = 0, n = strlen(argv[1]); i < n; i++)
        {
            if (isalpha(argv[1][i]))
            {
                if (isupper(argv[1][i]))
                {
                    tolower(argv[1][i]);
                }
            } else
            {
                printf("usage:./vigenere <keyword>\n");
                return 1;                
            }
        }

    }

    //get the plaintext
    string plain = GetString();
    //output the cipertext 
    int order = 0;
    for (int i = 0, n = strlen(plain); i < n; i++)
    {
        char letter = plain[i];
        char key = argv[1][order % strlen(argv[1])] - 'a';
        if (islower(letter))
        {
            letter = 'a' + (letter - 'a' + key) % 26;
            printf("%c", letter);
            order = order + 1;
        } else if (isupper(letter))
        {
            letter = 'A' + (letter - 'A' + key) % 26;
            printf("%c", letter);
            order = order + 1;
        }
        else
        {
            printf("%c", letter);
        }                

    }
    return 0;
}
1

When I run your code I get a floating point exception error which seems to lie in the first for loop. You don't want to put integer values in place of a char because of obvious reasons (just go look at the first 26 chars in an ASCII table). I would drop that and just figure the key in the same loop that you're enciphering the text.

So change this:

char key = argv[1][order % strlen(argv[1])];

To this:

char key = argv[1][order % strlen(argv[1])] - 'a';

Use tolower() on the first loop for the key and check if chars are alphabetic using isalpha().

for (int i = 0, n = strlen(argv[1]); i < n; i++)
{
    if (isalpha(argv[1][i])) {
        if (isupper(argv[1][i])) {
           argv[1][i] = tolower(argv[1][i]); // EDIT: you have to set argv[1][i] equal to the call to tolower()
        }                
    } else {
        printf("usage:./caesar <keyword>\n");
        return 1;
    }
}
8
  • It is still not working right. This time there's no "not standard error", but it cipher some letters to other ASCII values. I tried storing key as int, but it doesn't work either. – Kevin King Aug 8 '16 at 1:28
  • Could you edit your question and put your newly written code there too? – NullityNull Aug 8 '16 at 1:30
  • The code is updated. Thank you! – Kevin King Aug 8 '16 at 1:47
  • My apologies, I've made a mistake on setting the key string to lowercase. You have to set argv[1][i] equal to the call to tolower(). I've made the changes, re-ran your code, and it works perfectly passing all of the check50 tests. Check my answer edit. And if this answers your question, I'd appreciate it if you would accept the answer to keep up on forum maintenance. :-) – NullityNull Aug 8 '16 at 2:43
  • It finally worked! Thank you so much! But I still don't know why my initial code in the first for loop didn't work. Why " argv[1][i] -= 'a' " is not doing the same thing. argv[1][i] type is still char (or a number), the same as your code. Isn't it? – Kevin King Aug 8 '16 at 3:06

You must log in to answer this question.

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