1

Whenever I type plaintext into my vigenere cipher, it moves forward one letter when it encounters spaces or punctuation, even though the key does not affect those characters. How can I prevent this? Here's my code regarding spaces and punctuation:

if (ispunct(p[i]))
{
    printf("%c", p[i]);          
}

if (isspace(p[i]))
{
    printf(" ");
}

1 Answer 1

1

It wasn't completely clear, but it seems that you are having a problem with your key advancing when non-alpha letters are processed. If this is the case, the relevant code is what isn't shown. My best guess is that you are using the same index to advance through both your plaintext and your key. Because the key is not always used, (when non-alphas are processed in the plaintext), the key needs a separate and independent index variable that is only incremented when a letter is encoded.

BTW, what happens if there's a number in the plaintext in your code? Wouldn't it be a lot simpler to check if a letter isalpha(), or is not alpha?

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

4
  • 1
    Just put in the !isalpha and it works way better
    – K. Roe
    Oct 29, 2015 at 21:01
  • 1
    Here's the code for my for loop: for (i = 0, j = strlen(key); i < strlen(p); i++) and the variable I'm using is: int code = (p[i] + key[i % j]) % 26; does that mean I'm using the same index?
    – K. Roe
    Oct 29, 2015 at 21:04
  • 1
    "code" is for upper case letters; for lower case I'm using: int coded = (toupper(p[i]) + key[i % j]) % 26;
    – K. Roe
    Oct 29, 2015 at 21:05
  • 1
    Yes, it does. Since j=strlen(key), you need yet another var to step through key.
    – Cliff B
    Nov 1, 2015 at 6:51

You must log in to answer this question.

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