0

Okay, I'm working on the Vigenere code and all of it works but it's ability to printf to screen. No errors on the make, and when I do the check50 test I get this.

:) vigenere.c exists :) vigenere.c compiles :( encrypts "a" as "a" using "a" as keyword \ expected output, but not "\u0013\u0000" :( encrypts "world, say hello!" as "xoqmd, rby gflkp!" using "baz" as keyword \ expected output, but not "\u0010\b\u000b\u0005\u0017, \f\u0014\u0..." :( encrypts "BaRFoo" as "CaQGon" using "BaZ" as keyword \ expected output, but not "\u000fB\u000e\u0005R\u0013F\u0002\u0002..." :( encrypts "BARFOO" as "CAQGON" using "BAZ" as keyword \ expected output, but not "\u000fB\u000eA\u0005R\u0013F\u0002O\u00..." :) handles lack of argv[1] :) handles argc > 2 :) rejects "Hax0r2" as keyword

I'm starting to lose my mind a bit, cause a practically similar caesar for loop with if statements printed just fine.

Here's a bit of the code, and yes I've check the brackets to make sure that all got closed and matched.

if (isupper(p[i])) { int a = (p[i] + (key -65)) % 26; printf("%c", a );

                j++;
                }                   

      if (islower(p[i]))
                {
                int b = (p[i] + (key - 97)) % 26;
                printf("%c", b );

                j++;
                }   
      else
                {
                int c = p[i];    
                printf("%c", c );  
                }

Trying not to put too much up to avoid the "telling too much" but it you can tell what I am doing wrong please let me know, even if you know what the error codes mean would be great. Thanks

0

There's an old poem about computer programs, with a line that says, "Its doing exactly what I told it to do, but not what I want!"

It actually is printing the output. Unfortunately, the output consists of essentially non-printable codes, control codes, or graphics characters, depending on what was produced.

To understand, look at this line and work out what's happening with pencil and paper:

 int b = (p[i] + (key - 97)) % 26;

Say that p[i] = 'a' and key = 'z'. That means the following:

int b = (p[i] + (key - 97)) % 26;
      = ('a' + ( 'z' - 97 ) ) % 26
      = ( 97 + ( 122 - 97 ) ) % 26
      = ( 97 + ( 25 ) ) % 26
      = ( 122 ) % 26
      = 18

ASCII 18 is DC2 or Device Control 2 - hardly a printable character!

Remember what the formula needs to do. ALL of the ascii chars need to be converted to numbers from 0 to 25 inclusive, combined, modulo'd and then the result converted back to ASCII. I'll leave it to you to go back and sort it out. ;-)

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

  • Thank you so much!!! Very helpful. Was trying to do this math out like you did but didn't see how it was adding (literally) together correctly. And now for another round of math in my future. – Lisa Kucharski Jun 10 '16 at 20:22

You must log in to answer this question.

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