0

My Vigenere program is working pretty well. It's extremely similar to my code for Caesar, however it's printing the wrong text. Here's my code.

    int main(int argc, char *argv[])
    {
        if(argc < 2)
        {
            printf("waoh mahn!\n");
            return(1);
        }
        else if(argc > 2)
        {
            printf("waoh mahn!\n");
            return(1);
        }

        string key = argv[1];
        int c = 0;

        string input = GetString();
        for(int i = 0; i < strlen(input); i++)
        {
            if(isalpha(input[i]))
            {
                for(c = 0; c < strlen(key); c++)
                {
                    if(input[i] == 'z')
                        input[i] = 'a';
                    else if(input[i] == 'Z')
                        input[i] = 'A';
                    else input[i]++;
                }
            }
        }
        printf("%s\n", input);
    }

Here's the check50, for an example of what it prints.

:) vigenere.c exists
:) vigenere.c compiles
:( encrypts "a" as "a" using "a" as keyword
   \ expected output, but not "b\n"
:( encrypts "world, say hello!" as "xoqmd, rby gflkp!" using "baz" as keyword
   \ expected output, but not "zruog, vdb khoor!\n"
:( encrypts "BaRFoo" as "CaQGon" using "BaZ" as keyword
   \ expected output, but not "EdUIrr\n"
:( encrypts "BARFOO" as "CAQGON" using "BAZ" as keyword
   \ expected output, but not "EDUIRR\n"
:) handles lack of argv[1]
:) handles argc > 2
:( rejects "Hax0r2" as keyword
   \ expected output, not a prompt for input

I can't really seem to tell what the issue is here yet. I also haven't programmed it to reject Hax0r2 yet, but that won't be much of an issue.

1 Answer 1

1

This program isn't doing what was requested. Instead, it is using the length of the key string as the encoding key, much the same way that caesar did. To prove this, try running your code twice, once with key aaa and the second time with ghi, and both times using the same plain text string. The output will be the same. The output will be shifted by 3 letters from the input.

It is supposed to use each letter of the key to encode a letter of the plain text string. As each letter of the plain text is encoded, the next letter of the key is used to encode the next letter of the plain text string.

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

You must log in to answer this question.

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