0

I have been working on vigenere for a long time now, and here is my issue. plaintext cab with a keyword of ab returns ccbbbb. so, the plaintext is enciphering correctly but the last letter is repeating for as long as the plaintext? My only ideas as to why this is, is that either I am treating j (position in keyword) incorrectly, or my condition for looping through the plaintext is wrong. I have thoroughly checked both, but cannot seem to see the problem. Will post some of my code below. Any pointers in the right direction would be amazing, thank you thank you in advance.

// execute loop for every char of the plaintext
for (int i = 0, j = 0; i<plen; i++, j++)
{
// execute if char is alphabetical
    if (isalpha (p[i]))
    {
    // key wraparound
    if (j >= keylen) 
    j = 0;
        // encryption if char of plaintext and char of key are uppercase
        if (isupper (p[i]) && isupper (keyword[j]))                
        {
            // code here rotates plain char by key char and prints
        }
        // encription if char of plaintext and char of key are lowercase
        else if (islower (p[i]) && islower (keyword[j]))
        { 
          // code here rotates plain char by key char and prints
        }
        // encryption if plaintext char is uppercase and keyword char is lowercase
        else if (isupper (p[i]) && islower (keyword[j]))
        {
          // code here rotates plain char by key char and prints
        }
        // encryption if plaintext char is lowercase and keyword char is uppercase
        else if (islower (p[i]) && isupper (keyword[j]));
        {
          // code here rotates plain char by key char and prints
        }


    }
        // prints non-alphabetical plaintext char
        else
        {
            j = j - 1;
            printf("%c", p[i]);
        }

}

1 Answer 1

0

What little code is there looks right. I suspect the problem is in some part of the code that you didn't include. It looks like you are either executing two different printf() statements or the same one twice for each letter processed.

Try altering each printf() statement so that each one prints something unique along with the letter that it prints so that you can identify which ones are printing where and in what order. If these tips don't get you going, you will have to add more of the relevant code to the question so we can see what's broken.

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

1
  • Thanks for the tip on making unique printf statements! it turns out the problem was a semi colon after the line // encryption if plaintext char is lowercase and keyword char is uppercase else if (islower (p[i]) && isupper (keyword[j])); not sure why, but removing that fixed it!
    – Emma H
    Commented Aug 21, 2015 at 18:54

You must log in to answer this question.

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