0

I wrote my vigenere cipher program and everything works fine, but when I run check50 I get the following error:(p is the plaintext string)

char c[strlen(p)];
for(int i = 0, j = 0, l = strlen(p), n = strlen(argv[1]); i < l; i++)
{
    j %= n;
    if(isupper(p[i]))
        {
            c[i] = (((int)p[i] - 65 + kn[j]) % 26) + 65;
            j++;
        }
    else if(islower(p[i]))
        {
            c[i] = (((int)p[i] - 97 + kn[j]) % 26) + 97;
            j++;
        }
    else 
        c[i] = p[i];
}
printf("%s\n", c);

:( encrypts "a" as "a" using "a" as keyword
 \ expected output, but not "aü\u0005uÿ\n"
:( encrypts "world, say hello!" as "xoqmd, rby gflkp!" using "baz" as keyword
 \ expected output, but not "xoqmd, rby gflkp!D\nVÿ\n"

However,

:) encrypts "BaRFoo" as "CaQGon" using "BaZ" as keyword

This is the part of my code that makes the cipher text:

I can't figure out if I go beyond the array indexing because this doesn't happen with other check50 tests.

Thanks in advance!

2

Uhhuh. I see that it is doing a nice job of encoding and storing the encoded characters in an array. But have you also stored the end of string marker '\0' at the end of the string?

printf will keep printing whatever it finds in memory until it finds that null character which marks the end of the string.

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

| improve this answer | |

You must log in to answer this question.

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