this was very useful last time when I was struggling with an unseen error. I'm just after a nudge in the right direction again, My code compiles and passes most of the check50 tests bar a few, the check50 report is as follows;

:) 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 "ciphertext: xoqmd, szz gflkp!\n"

:( encrypts "BaRFoo" as "CaQGon" using "BaZ" as keyword

\ expected output, but not "ciphertext: CaQGoh\n"

:) encrypts "BARFOO" as "CAQGON" using "BAZ" as keyword

:) handles lack of argv[1]

:) handles argc > 2

:) rejects "Hax0r2" as keyword

It's strange how close the rejected answers are to the correct ones. I don't have anywhere near as much code as other examples I've seen of this problem which worries me, my core code for calculating the ciphertext is as follows;

for (int i = 0, n = strlen(p); i < n; i++)

    char c = p[i];
    char kc = k[i % strlen(k)]; 

    if isalpha(c)                           

        if isupper(c)                       
            c = c - 65;
            kc = kc- 65;
            printf("%c", (c + kc)%26 + 65);  

        else if islower(c)                  
            c = c - 97;
            kc = kc - 97;
            printf("%c", (c + kc)%26 + 97);
            printf("%c", c);               


I've been trying to avoid redundancy and keep it as neat as possible, but am I missing something? I spent a fair while jotting down pseudocode but feel that I may have messed up a loop somewhere with relation to the k[ i % strlen(k) ], any help would be greatly appreciated.


1 Answer 1


The index inside the keyword should be incremented only when encrypting a letter. It has to be kept separate from the index in the plaintext, which advances on any character.

Case of character to be encrypted is independent of case in keyword. For simplicity, you could ignore the case in the keyword by writing kc = tolower(kc) - 97; or similar.

  • Ah yes I see this after working through it manually that my key position is progressing regardless of character, so I will need to introduce a new variable to track the key position. I was thinking of just adding in int j = 0; at the start of the loop, and having j++; for the cases where its an alpha character, would this work? And yeah big error there not realising that isupper will effect both the key character and the plain text character, i'll look into the reference guide for help, thank you! Feb 14, 2017 at 14:04

You must log in to answer this question.

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