0

I can't get my Vigenere code to work, my math isn't that great and I'm losing grip on the formula. Caesar was do-able for me but this one is just not working. Can anyone see what's wrong with my code?

printf("plaintext: ");
string key = argv[1];
int length = strlen(key);
string s = get_string();
int j = 0;
printf("ciphertext: ");

for (int i = 0; i < strlen(s); i++)
{
    if(isalpha(s[i]))
    {
        if(isupper(s[i]))
        {
            int x = (((s[i]- 65 + key[j] - 65) % 26) + 65);
            printf("%c", x);
        }
        else if(islower(s[i]))
        {
            int y = (((s[i]- 97 + key[j] - 97) % 26) + 97);
            printf("%c", y);
        }
    }
    else
    {
        printf ("%c", s[i]);
    }

    j++;
    if (j == length)
    {
        j = 0;
    }
}
printf("\n");
}
0

You're actually really close to being finished.

Your formulas for encoding the letters are correct.

However, there are a couple of other errors. First, the index for the key, j, is being incremented for every char. It should only be incremented when a letter is encoded, so move that code inside the if statement code blocks.

Second, there is no code to handle the situation where the plaintext char is not the same case as the key. What happens when one is upper case and the other is lower case?

As a side note, if a char isupper() or islower(), it is by definition isalpha(). That means that checking for isalpha() is redundant and not needed.

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

4
  • I get your first argument, but I don't know what to do about the second problem. You're saying I should write like four if loops with: if (plaintext is uppercase and key is uppercase, if plaintext is uppercase and key is lowercase, ...) ?
    – Julia
    Sep 14 '17 at 20:48
  • correct. there are 4 cases, so that means 4 if statements, not 2.
    – Cliff B
    Sep 14 '17 at 20:52
  • I have now tried that but it doesn't seem to be working correctly.. Unfortunately I can't copy the whole code into this comment...
    – Julia
    Sep 14 '17 at 20:57
  • I have asked another question with my updated code in it. Maybe you can help me further?
    – Julia
    Sep 14 '17 at 21:11
-1

In these 2 expressions:

int x = (((s[i]- 65 + key[j] - 65) % 26) + 65);

int y = (((s[i]- 97 + key[j] - 97) % 26) + 97);

looks like you are doing -65 (or -97) one time too many. For instance, if your character is a and the key is, say, 11. You are doing: 'a' - 65 + 11 - 65 which is now negative. Then % 26 would give you a negative number

Another problem I can see is this: You are incrementing j everytime you loop through the characters of the string. So even if the character is not alphabetical (e.g.: a space), you would still use the next character in the key. That would be wrong per the vigenere requirement. You might want to try putting j++ within the

if (is alpha(s[I]))

condition only.

I can't rewrite your code but those are 2 things I could see

4
  • You're half right. The j++ issue is correct. However, the formulas are also correct.
    – Cliff B
    Sep 14 '17 at 17:27
  • That was true for Caesar, but in vigenere, the keys are alpha, not numeric. Thus, the keys must also have 65 or 97 subtracted from them.
    – Cliff B
    Sep 15 '17 at 2:46
  • Have you considered not down voting anything that is not correct the first time without explaining?
    – buitri84
    Sep 15 '17 at 3:05
  • I always consider downvotes carefully. In this case, the answer had bad info. If something is clearly wrong, a downvote is warranted. If corrected, removing a downvote is also an appropriate response. You always have the option of deleting the answer, which will remove the downvote, or correcting the answer, which will give me reason to consider whether to remove the downvote. It isn't personal. Anyone that's reasonably active here probably gets downvoted sooner or later, myself included.
    – Cliff B
    Sep 15 '17 at 3:23

You must log in to answer this question.

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