I am working on the Vigenere portion of the pset and appear to have it totally working, yet when I ran the checker I realized that I had not created a test for non-alphabet characters in the command line input, I had only put this:
if (argc != 2)
{
printf("DID NOT INPUT CORRECTLY");
return 1;
}
Great, so I go back and right after that, I add the following loop to check for only alphabetic characters in the command line input:
for (int c = 0, d = strlen(argv[1]); c < d; c++)
{
if(!isalpha(argv[1][c]))
{
printf("DID NOT INPUT CORRECTLY");
return 1;
}
}
It checks for non-alphabet characters correctly, but for some reason now in some instances my phrase is no longer being encrypted correctly! Does anyone know how the above code could be causing unintended changes/consequences?
For reference if it helps, here is the rest of my code:
string phrase = GetString();
int codelength = strlen(phrase);
int keylength = strlen(argv[1]);
char encrypted[codelength];
char encoder[codelength];
for (int a = 0, b = strlen(phrase); a < b; a++)
{
int spot = a % keylength;
encoder[a] = argv[1][spot];
}
int key = 0;
int j = 0;
for (int i = 0, p = strlen(phrase); i < p; i++)
{
if (encoder[j] >= 'A' && encoder[j] <= 'Z')
{
key = encoder[j]-65;
}
else if (encoder[j] >= 'a' && encoder[j] <= 'z')
{
key = encoder[j]-97;
}
if ((phrase[i] >= 'A' && phrase[i] <= 'Z') || (phrase[i] >= 'a' && phrase[i] <= 'z'))
{
if (phrase[i] >= 'A' && phrase[i] <= 'Z')
{
if (phrase[i] + key <= 'Z')
{
encrypted[i] = phrase[i] + key;
}
else
{
encrypted[i] = 65 + key - (91 - phrase[i]);
}
}
else if (phrase[i] >= 'a' && phrase[i] <= 'z')
{
if (phrase[i] + key <= 'z')
{
encrypted[i] = phrase[i] + key;
}
else
{
encrypted[i] = 97 + key - (123 - phrase[i]);
}
}
j++;
}
else
{
encrypted[i] = phrase[i];
}
}
printf("%s\n",encrypted);