0

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);

2 Answers 2

0

Interesting problem. Adding the new code wasn't the cause, but it exposed a hidden problem. If you look carefully at the results, you'd see that the encryption is being done correctly, but the real issue is that extra characters are being added at the end. (A lesson that errors need to be precisely analyzed and described. ;-) )

Now, look at what prints out the result: printf("%s\n",encrypted); What determines the end of the string that has to be printed? It's the end of string marker, '\0'. But when was this added to the encrypted string? Since it isn't there, the printf statement is going to keep going until it finds what it thinks is the EOS marker. Also, the length of encrypted doesn't allow for the addition of the end of string marker. Same thing applies to encoder.

I'll leave it to you to figure out why adding the new code exposed the problem. ;-)

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

3
  • So I figured out how to make the code work and it now checks correctly, I simply added the following: encoder[codelength] = '\0'; encrypted[codelength] = '\0'; I am still a little confused as to why those two string variables required the manual addition of a \0 at the end. Is this necessary any time I create a string that was not created via GetString()?
    – mlh
    Dec 8, 2016 at 22:16
  • you added.... what?
    – Cliff B
    Dec 8, 2016 at 22:17
  • Sorry about that I hit enter too quickly. I also changed the length of encoder and encrypted to [codelength+1]
    – mlh
    Dec 8, 2016 at 22:18
0

For non-alpha characters the shift adjustment is 0, rather than 65 & 97. You could add this 0 shift in a similar way to the other adjustments.

If you follow through the 'if' and 'else' commands, the section

else
{
    encrypted[i] = phrase[i];
}

doesn't follow through correctly. Try to do the non-alpha adjustments in the same manner as the lowercase and uppercase adjustments.

I suggest you try the 'islower' and 'isupper' functions, which are more concise.

Good luck. :-)

1
  • I definitely see now how using islower and isupper would make it more concise, thank you!
    – mlh
    Dec 8, 2016 at 22:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .