0

Im trying to figure out Vigenere from pset2. My program doesn't print out the letters of my plaintext more than once, it prints the fist letter no problem but doesn't print the second, third, ect. It did print at one point but it had errors and in the midst of trying to fix those errors i produced more =[.

Here is my code i really hope someone can help.

for (int i = 0, pl = strlen(p); i < pl; i++)
        {   
          j = index % kl;
          char current = p[i];
          if (isupper(current))
          {   
                if(isupper(k[j]))
                {   
       int highresult = ((current - 'A') + ( k[j] - 'A')) % 26 + 'A';
                    printf("%c", highresult);
                    index++;
                }

            }

            else if(isupper(current))
            {
                if (islower(k[j]))  
                {
                    int lowresult = (current - 'a') + ( k[j] - 'a') % 26 + 'a';
                    printf("%c", lowresult);
                    index++;
                }
            }


            else if (islower(current))
            {
               if(isupper(k[j]))
               {
                   int highresult = ((current - 'A') + (k[j] - 'A')) % 26 +'A';
                   printf("%c", highresult);
                   index++;

               }
            }   

            else if (islower(current))
            {
                if(islower(k[j]))
                {
                    int lowresult = (current - 'a') + (k[j] -'a') % 26 + 'a';
                    printf("%c", lowresult);
                    index++;
                }
            }

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

the Code is only my for loop as you can see. if there is anything else you need to help me just ask please!

Thanks!

0
0

Your problem is with your ifs. If an if case is found to be true, the others are not checked. So if your first

if (isupper(current))

is true the following

else if(isupper(current))

will never be checked.

So if the plaintext is uppercase but the key is lowercase, you will not get any result. The solution would be to not use two different if(isupper(current)). Use instead:

if (isupper(current))
{
    if (isupper(k[j]))
    {
        // do things here
    }
    else
    {
        // do other things here
    }
}
1
  • 1
    Hi Chris. Thanks for replying i'm sorry for taking so long to get back to you. i took your advice and it now works great! You helped a lot. thanks. – Jay Jay Aug 6 '15 at 18:07

You must log in to answer this question.

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