After many hours of tinkering and research, I still don't grasp why my vigenere code fails check50 on these two accounts:
1) encrypts "world, say hello!" as "xoqmd, rby gflkp!" using "baz" as keyword
2) encrypts "BaRFoo" as "CaQGon" using "BaZ" as keyword
https://sandbox.cs50.net/checks/f5d3393fa6a5413cb1aa4c5b3965beeb
My code:
if (argc != 2)
{
printf("Please enter a single key word at the command line.\n");
return 1;
}
else if (argc == 2)
{
for (int j = 0, n = strlen(argv[1]); j < n; j++)
{
int flag = 0;
if (isalpha(argv[1][j]))
{
flag = 0;
}
else
{
flag = 1;
printf("Please enter only alphabetic characters.\n");
return 1;
}
}
// printf("Please enter some plain text to encrypt.\n");
string p = GetString();
string k = argv[1];
for (int i = 0, pl = strlen(p), kl = strlen(k), j = 0; i < pl; i++)
{
char curr = p[i];
if (isalpha(curr) && isupper(curr))
{
int c = i % kl;
int key_char = k[c] - 65;
int alpha = p[i] - 65;
int alpha_adj = (alpha + key_char) % 26;
int ascii = alpha_adj + 65;
printf("%c", ascii);
j = (j + 1) % kl;
}
else if (isalpha(curr) && islower(curr))
{
int c = i % kl;
int key_char = k[c] - 97;
int alpha = p[i] - 97;
int alpha_adj = (alpha + key_char) % 26;
int ascii = alpha_adj + 97;
printf("%c", ascii);
j = (j + 1) % kl;
}
else
{
printf("%c", curr);
}
}
printf("\n");
return 0;
}
Up-voted responses to other similar questions here on cs50.stackexchange have suggested that including printf() prompts that are not explicitly required in the specs will cause certain check50 failures.
However, I've tried removing those prompts in my code, but each time I do it just results in other check50 failures that seemingly have no logical connection to them. In general, I'm unable to discern from sandbox why these failures are occurring and what corrective action to take. I'm fresh out of ideas.