0

I think i almost did vigenere pset. but output is strange. in 'baz' key: b is correct it increment 1 point but also a increment 1 point and z increment 1 point instead of subtract 1.. İt doesn't make any sense to me. I almost give 1 hours only this situation. I tried many things but again same thing.. here is my code's related parts.

for (int j=0, i = 0; i < n; i++)
    {
        //Key's loop to iterate the key word
        if (isalpha(p[i]))
        {
        j = j % m;
        }

        if (p[i] >= 'a' && p[i] <= 'z')
        {         

         if(p[i] + k[j] > 122)
             {
             p[i] = p[i] - 26;
             } 

         printf("%c", p[i] + k[j]);
         j++;
         }
         else if (p[i] >= 'A' && p[i] <= 'Z')
         {
             if(p[i] + k[j] > 90)
             {
             p[i] = p[i] - 26;
             }
         printf("%c", p[i] + k[j]);
         j++;
         }
         //For non-alphabet numbers which should not change
         else
         {
         printf("%c", p[i]);
        }
     }
     printf("\n");

}

void keyFunction(string name)
{
    for ( int i = 0 , n = strlen(name); i < n; i++)
    {
        if (name[i] >= 'a' && name[i] <= 'z')
        {
        name[i] = name[i] % 'a';
        }
        else if (name[i] >= 'A' && name[i] <= 'Z')
        {
        name[i] = name[i] % 'A';
        }     
    }
}
1
0

Consider using the modulo operator. We can use this to apply a range to our formula. It returns the remainder of a division. 7 / 3 = 2 (remainder 1). Therefore 7 % 3 = 1. Thus x % 3 can never be larger than 2 (any whole number divided by 3 can only have the remainder of 0 (no remainder), 1 or 2.

Could we use this to ensure that, no matter which number we use in our formula, it returns 0 to 25?

Furthermore. To iterate through your key's index you are using a variable (j)that increases for each run of the loop. What if the text we want to encrypt includes a symbol, should we still increment the index of our key?

You must log in to answer this question.

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