0

I'm working on the Vigenere problem and I've came up with the following code:

 for (int i = 0, j= 0, p = strlen(s); i < p; i++, j++) 
 {
   int b= (j % strlen(argv[1])); 
   if (isupper(s[i]))
   {
       if (isupper(argv[1][b]))
       {
       printf ("%c", ((((s[i] - 65) + (argv[1][b] - 65)) % 26) + 65)); 
       }
       else
       {
       printf ("%c", ((((s[i] - 65) + (argv[1][b] - 97)) % 26) + 65)); 
       }
   }

   if (islower(s[i]))
   {
       if (isupper(argv[1][b]))
       {
       printf ("%c", ((((s[i] - 97) + (argv[1][b] - 65)) % 26) + 97)); 
       }
       else
       {
       printf ("%c", ((((s[i] - 97) + (argv[1][b] - 97)) % 26) + 97));            }
   }
   if(!isupper(s[i]) && !islower(s[i]))
   {
     printf("%c", s[i]);
   }

However, the program applies the Vigeneres's cipher to all characters, including numbers and spaces. When the plaintext is:" Meet me at the park at eleven am", I get the ciphertext: "Negh ne og tjs qaty bt syfvgb bm". Could somebody help me? Thank you.

1

The j++; should go into the isupper(s[i]) and islower(s[i]) paths. Alternatively, add a compensating j-- in the if(!isupper(s[i]) && !islower(s[i])) path.

Your last if would not be necessary if you used a few else instead, like if ... else if ... else, or nesting, like if (isalpha(s[i])){ if(isupper(s[i]))...else... } else...

You must log in to answer this question.

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