I have gone through many of the stack and reddit questions concerning the Vigenere problem. I am so close to figuring it out I can taste it!
I have been working through the issue of the BaZ key for the last few hours and am ready to pull my hair out. My cs50 check returns:
:( encrypts "BaRFoo" as "CaQGon" using "BaZ" as keyword
\ expected output, but not "CBzSREpon\n"
I have checked for the key being upper and lower case, and have been through everything as logically as I can. At this point, I've been staring at my screen for too long and am missing something.
This is my code:
for (i = 0, l = strlen(p); i < l; i++)
{
if (isalpha(p[i]))
{
if (isupper(p[i]) && isupper(k[n]))
{
int c = (p[i] - 65 + ((k[n]) - 65)) % 26;
printf("%c", c + 65);
n++;
}
if (isupper(p[i]) && islower(k[n]))
{
int c = (p[i] - 65 + (k[n] - 97)) % 26;
printf("%c", c + 65);
n++;
}
if (islower(p[i]) && islower(k[n]))
{
int c = (p[i] - 97 + (k[n] - 97)) % 26;
printf("%c", c + 97);
n++;
}
if (islower(p[i]) && isupper(k[n]))
{
int c = (p[i] - 97 + ((k[n]) - 65)) % 26;
printf("%c", c + 97);
n++;
}
if (n == g)
n = 0;
}
if (!isalpha(p[i]))
{
printf("%c", p[i]);
}
}
printf("\n");
return 0;
What am I missing? Someone please help me out before I go bald!
You are giving more than one output for one letter. You have to use "else if" after first "if"s. Or make that n++ at the end of the first big if block. Incrementing n at the first internal if block may cause it enters second if block too because k[n] changes.