0

my vigenere code only runs through the keyword once, like when I use the keyword BACON for the plain text ENDURANCE it encrypts as FNFIE...the keyword doesn't wraparound the plain text continously

for(int i = 0,j = 0;j < key_length && i <         p_text_length; i++)
{
    if(isupper(key[j]))
    {
        keytext = (key[j] - 'A')%26;

        if(islower(plaintext[j]))
        {
            ciphertext = ((plaintext[i] - 'a' + keytext) % 26) + 97;
            printf("%c", ciphertext);
        }
        else if(isupper(plaintext[j]))
        {
            ciphertext = ((plaintext[i] - 'A' + keytext) % 26) + 65;
            printf("%c", ciphertext);
        }
        else if(!isalpha(plaintext[j]))
        {
            ciphertext = plaintext[i];
            printf("%c", ciphertext);
        }
        j++;
    }

    else if(islower(key[j]))
    {
        keytext = (key[j] - 'a');

        if(islower(plaintext[j]))
        {
            ciphertext = ((plaintext[i] - 'a' + keytext) % 26) + 97;
            printf("%c", ciphertext);
        }
        else if(isupper(plaintext[j]))
        {
            ciphertext = ((plaintext[i] - 'A' + keytext) % 26) + 65;
            printf("%c", ciphertext);
        }
        else if(!isalpha(plaintext[j]))
        {
            ciphertext = plaintext[i];
            printf("%c", ciphertext);
        }
        j++;
    }
1
  • We need more info to help. Can you post the part of your code that wraps back around the key? But without seeing it, I would suggest that you look at how you're cycling through the key and, most importantly, resetting back to the start of the key. Try working it on paper and see what it does. Also, either print out the key character for each letter to be encoded or track it in gdb.
    – Cliff B
    May 26 '15 at 6:16
1

You're exactly right. For the keyword BACON, this code will process exactly 5 characters (the number of characters in BACON) and exit the loop. Here's why. Look at the control statement for the loop:

for(int i = 0,j = 0;j < key_length && i < p_text_length; i++)

There are two controls in place here. One is the length of the text to translate, the other is the length of the key. Whichever one gets hit first will terminate the loop.

Once that is fixed, there's still another issue. There's no mechanism here to wrap back around to the beginning of the key.

You're on the right track. You know that the length of the string to be encoded is a limit. You also realized that the length of the key is important. The problem here is that the code is written to terminate when the end of the key is reached. Instead, the code needs to go back to the beginning of the key and reuse it, making the key look something like "BACONBACONBACONBAC..."

You need a couple of design changes. First, the key length cannot be a control in the for loop. If the key is shorter than the string to be encoded, it will never encode the whole string. (Initializing j in the control statement is OK though.) Making this change will stop the encoding from terminating early. The statement becomes this:

for(int i = 0,j = 0; i < p_text_length; i++)

Next, you need to figure out how to go back to the beginning of the key after the last character has been used. Currently, you have the statement(s) j++ to step through the key. For BACON, with a length of 5, what happens when j increments to 5? Remember, this is 0 indexed, so 0 through 4 are the valid numbers.

It would be easy to tell you the fix, but more important that you figure it out yourself. For the specific case of BACON, what operator can you use to wrap around back to 0, when j increments beyond 4? In the general case, what is that operator and what is the value or limit to be used? You've actually used the operator already.

Here's a hint. j++ ; becomes something like this:

j = ++j <some operator> limit_value;

Note: ++j works here, so does (j+1), but j++ doesn't. I'll let you figure out why.

You should be able to figure it out from here, but if you can't, leave a comment and we can help get you the rest of the way. ;-) This should get you going though.

If this answers your question, please accept this answer thus marking this question as answered. Let's keep up on forum housekeeping. ;-)

6
  • Thanks but using j = j++ gives a multiple unsequenced modifications to variable j error Jun 13 '15 at 9:31
  • Well, I did say that it wouldn't work. I also said that there's more to the statement than j = ++j;
    – Cliff B
    Jun 13 '15 at 16:09
  • Thanks I tried it that way also and got the same error Jun 13 '15 at 19:01
  • So I guess you didn't understand what I had said. Just to make sure that there are no errors, try this: ` j = (j + 1) % keylength; ` When j gets to the end of the key, it will reset to 0.
    – Cliff B
    Jun 13 '15 at 19:14
  • Thanks @Cliff B....j = (j+1)% limit_ value.....actually did the trick... Jun 13 '15 at 19:21

You must log in to answer this question.

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