I've been working on vigenere.c for awhile but I can't get the encryption right for the life of me. This is the closest I've gotten, but I keep getting this error when I use check50:

:) 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 "xoqmd, szz gflkp!\n"
:( encrypts "BaRFoo" as "CaQGon" using "BaZ" as keyword
\ expected output, but not "CaQGoh\n"
:) encrypts "BARFOO" as "CAQGON" using "BAZ" as keyword
:) handles lack of argv[1]
:) handles argc > 2
:) rejects "Hax0r2" as keyword

I can't figure out why some of it encrypts properly and some of it doesn't, or why \n is showing up at the end of some encryptions, so a nudge in the right direction would be awesome. Here's the relevant part of my code:

// declare key variable
string key = argv[1];

// get text to encrypt from user
string text = GetString();

// encryption
for (int i = 0, n = strlen(text); i < n; i++)
    // check if alphabetical
    if (isalpha(text[i]))
        // check if lowercase
        if (islower(text[i]))
            int keyletter = key[i % strlen(key)] - 97;
            int cipher = ((((text[i] - 97) + keyletter) % 26) + 97);
            printf("%c", cipher);     
        // check if uppercase
        if (isupper(text[i]))
            int keyletter = key[i % strlen(key)] - 65;
            int cipher = ((((text[i] - 65) + keyletter) % 26) + 65);
            printf("%c", cipher);
    // print non-alphabetical characters
        printf("%c", text[i]);

// print new line

return 0;


1 Answer 1


You have the two most common problems with this pset.

First, you can't index through the key using the same index as you use for the plaintext. The index for the key should only be incremented when a letter is encoded, not when a non-alpha is processed. It needs a separate index variable.

Second, your code assumes that the key and the plaintext are the same case. It won't properly handle the situation where one is upper case and the other is lower case. You need to add this to the code.

As for the '\n', it's supposed to be at the end of your output. I think it looks different to you because when it's right, the test spec shown is a canned string that doesn't have it, while the actual output that your program produces contains it. I think that you're correct with the \n. However, once everything else is fixed, if you still have errors, then look into it.

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

  • Thanks for the answer! I managed to get the program to iterate separately through the plaintext and the key, but I'm still having problems with the second issue. I know you're supposed to convert the key to either all uppercase or all lowercase but nothing I do seems to be working. Any hints you could give me with that?
    – Danielle R
    Sep 22, 2015 at 22:06
  • There are a number of different ways to handle it. You can have 4 separate blocks of code to handle each different case combination. Or, a simpler method would be to convert the key string elements to numbers between 0 and 25 before you start to process the plain text string.
    – Cliff B
    Sep 22, 2015 at 22:19

You must log in to answer this question.

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