0

I've been working on this pset for a few weeks now and can't seem to figure out how to correctly apply modulo to wrap my cipher. When I run my program, I correctly cipher the first time around, think I correctly advance only for chars, but then I'm stuck...any thoughts/advice?

   for (int i = 0, count = 0, j = strlen(text); i < j; i++)  //for each letter of original text
{
    if (isalpha(text[i]))
    {

        if (isupper(text[i]))    //TEXT IS UPPER
        {
            if (isupper(key[i]))     // key is UPPER
                {
                    int shift = (key[count] - 'A') % 26;
                    printf("%c", (((text[i] - 'A') + shift) % 26) + 'A');

                }

            if (islower(key[i]))  //key is lower
                {
                    int shift = (key[count] - 'a');
                    printf("%c", (((text[i] - 'A') + shift) % 26) + 'A');

                }
        }    

        else if (islower(text[i]))   //apply shift to lower letters
        {
                if (isupper(key[i]))     //print uppers
                {
                    int shift = (key[count] - 'A') % 26; 
                    printf("%c", (((text[i] - 'a') + shift) % 26) + 'a');
                }

                if (islower(key[i]))  //print lowers
                {
                    int shift = ((key[count] - 'a') % 26); 
                    printf("%c", (((text[i] - 'a') + shift) % 26) + 'a');
                }

        }

    count++;    
    count = count % len;

    }
   if (!isalpha(text[i]))
    {
        printf("%c", text[i]);
    }

}
0

Why are you using i for the index of key in the tests and then using count (correctly) to calculate the shift?

if (isupper(key[i]))

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

You must log in to answer this question.

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