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);
}
}
else
{
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.
Cheers!