Right gentlemen. I've been really bad doing vigenere.c, doing it for 3 days in a row, I've finally made it and it passes check50 tests, but! I've 2 almost the same codes, one which should work, but it doesn't and another one which is working, with 2 more variables which we are not using in a code. What's wrong with me? This one is working
int main (int argc, string argv[])
{
bool argvCheck;
string vigKey = "";
// Checking correctness of <key> and argc arguments
do
{
if (argc != 2)
{
printf ("Something went wrong. ./vigenere <Key>\n");
return 1;
}
else if(argv[1])
{
for(int i = 0; i < strlen(argv[1]); i++)
{
if(!isalpha(argv[1][i]))
{
printf("Only letters should be used as a <Key>\n");
return 1;
}
else
{
argvCheck = true;
}
}
}
}
while (argvCheck == false);
vigKey = argv[1];
for(int i = 0; i < strlen(vigKey);i++)
{
vigKey[i] = tolower(argv[1][i]);
}
string plainText = GetString();
for (int i = 0, ar = 0; i < strlen(plainText); i++)
{
if(isalpha(plainText[i]))
{
if (isupper(plainText[i]))
{
printf ("%c", ((plainText[i] - 'A') + (argv[1][ar % strlen(argv[1])] - 'a')) % 26 + 'A');
}
else if (islower(plainText[i]))
{
printf ("%c", ((plainText[i] - 'a') + (argv[1][ar % strlen(argv[1])] - 'a')) % 26 + 'a');
}
ar++;
}
else
{
printf ("%c", plainText[i]);
}
}
printf ("\n");
return 0;
}
And this one is not working
int main (int argc, string argv[])
{
bool argvCheck;
//string vigKey = "";
// Checking correctness of <key> and argc arguments
do
{
if (argc != 2)
{
printf ("Something went wrong. ./vigenere <Key>\n");
return 1;
}
else if(argv[1])
{
for(int i = 0; i < strlen(argv[1]); i++)
{
if(!isalpha(argv[1][i]))
{
printf("Only letters should be used as a <Key>\n");
return 1;
}
else
{
argvCheck = true;
}
}
}
}
while (argvCheck == false);
//vigKey = argv[1];
/*for(int i = 0; i < strlen(vigKey);i++)
{
vigKey[i] = tolower(argv[1][i]);
}*/
string plainText = GetString();
for (int i = 0, ar = 0; i < strlen(plainText); i++)
{
if(isalpha(plainText[i]))
{
if (isupper(plainText[i]))
{
printf ("%c", ((plainText[i] - 'A') + (argv[1][ar % strlen(argv[1])] - 'a')) % 26 + 'A');
}
else if (islower(plainText[i]))
{
printf ("%c", ((plainText[i] - 'a') + (argv[1][ar % strlen(argv[1])] - 'a')) % 26 + 'a');
}
ar++;
}
else
{
printf ("%c", plainText[i]);
}
}
printf ("\n");
return 0;
}
As you can see there, if I get rid of vigKey, check50 says that I'm an idiot giving me these 2 errors
:( encrypts "BaRFoo" as "CaQGon" using "BaZ" as keyword
\ expected output, but not "=aKAoh\n"
:( encrypts "BARFOO" as "CAQGON" using "BAZ" as keyword
\ expected output, but not "=;KA/H\n"