0

This is the cleanest way i can think of of implementing vigenere, but i don't see whats wrong.. the code should work fine.. but its not. Please help

string k = argv[1];
int keyLength = strlen(k);
int keyCodes[keyLength];
int keyCount = 0;

printf("Please enter your plaintext: \n");
string plainText = GetString();

for(int i = 0; i < keyLength; i++)
{
    keyCodes[i] = toupper(k[i] - 65);
}

for(int i = 0, n = strlen(plainText); i < n; i++)
{
    if(!isalpha(plainText[i]))
    {
        printf("%c", plainText[i]);
    }
    else
    {
        printf("%c", caesar(plainText[i], keyCodes[keyCount]));

        if(keyCount < keyLength - 1)
     {
         keyCount++;
     }
     else
     {
         keyCount = 0;
     }
    }
}

printf("\n");   
return 0;
}

char caesar(char currentKey, int key)
{
   if(isupper(currentKey))
   {
       return((((currentKey - 65) + key) %26) + 65);
   }
   else
   {
       return((((currentKey - 97) + key) %26) + 97);
   }

}

this is just the part that handles the ciphering

It compiles but its outputs are all wrong..

Here is the outputs:

jharvard@appliance (~/Dropbox/workspace/pset2): ./vigenere bob Please enter your plaintext: foo bar plaintext miv iuy wfhphalra

But the output should be :gcp cos qzbjbuflu

1 Answer 1

0

There's an error in your code, but in the interest of giving you more experience in debugging, try this. Add a printf statement to the end of your first for loop to print out the result of converting your key letters to numbers.

    printf("Letter = %c, keycodes[%i] = %i\n", k[i], i, keyCodes[i]);

Then, compile, run and enter AaZz as your key and examine the output. It should generate 0, 0, 25 and 25 as the numeric keys, but doesn't. If you don't immediately know why, try plugging the actual ascii values for the letters into the conversion code.

Surprised? Now take a very careful look at the line of code that does the conversion.If you don't immediately know why, try plugging the actual ascii values for the letters into the conversion code.

There may or may not be other issues, but this is the big problem.

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

1

You must log in to answer this question.

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