I've been stuck on vignere.c for quite some time and I can't figure out what is wrong with my code. I received the following errors:
encrypts "world, say hello!" as "xoqmd, rby gflkp!" using "baz" as keyword
Expecting the following on standard out — xoqmd, rby gflkp!\n ... but received the following on standard out instead — xpsme, tbz ifmmp!
encrypts "BaRFoo" as "CaQGon" using "BaZ" as keyword
Expecting the following on standard out — CaQGon\n ... but received the following on standard out instead — CSGPP
encrypts "BARFOO" as "CAQGON" using "BAZ" as keyword
Expecting the following on standard out — CAQGON\n ... but received the following on standard out instead — CBSGPP
Here is the full check50 output.
I even tried doing my formula with pen and paper and it seems to be checking out but I can't figure out why the output is incorrect.
int main (int argc, string argv[])
{
if (argc != 2)
{
printf("Try again. Please enter a keyword.\n");
return 1;
}
for (int i = 0; i < strlen(argv[1]); i++)
if (!isalpha(argv[1][i]))
{
printf("Try again. Please enter only letters.\n");
return 1;
}
string plaintext;
plaintext = GetString();
int j=0;
string k=argv[1];
for (int i = 0;i < strlen(plaintext); i++)
{
if (j>=strlen(k))
{
j = 0;
}
if (!isalpha(plaintext[i]))
{
printf("%c", plaintext[i]);
}
if (isalpha(plaintext[i]))
{
if(isupper(plaintext[i]))
{
int ciphertext = (plaintext[i]-'A'+(k[j]-'A'))%26+'A';
printf("%c", ciphertext);
}
else if (islower(plaintext[i]))
{
int ciphertext = (plaintext[i]-'a'+(k[j]-'a'))%26+'a';
printf("%c", ciphertext);
}
else
{
printf("%c", plaintext[i]);
}
}
}
printf("\n");
return 0;
}