0

Having an issue with my formula. I don't think it is wrapping around the key word like its supposed to. through the first pass of the keyword it seems the encoding is correct but after that it is not enciphering correctly. Any help would be greatly appreciated!

int j = 0;

for (int i = 0, n = strlen(text); i < n; i++)
{
        if (isupper(text[i]))
        {  
            int KeyIndex = argv[1][j] - 65;
            int UserIndex = text[i] - 65;
            int cipher = (UserIndex + KeyIndex) % 26;
            int ToAscii = cipher + 65;

            printf("%c", ToAscii);                
        }
        j++;        
}
1

Based on the code shown, you're correct that the key isn't wrapping around. It's because there is no code to do that. Let's say that the key is "BaZ". That means that the key has a length of 3, implying the array elements 0, 1 and 2. When j gets to 3, the code just keeps incrementing beyond the end of the array. When J gets to the limit, 3 in this case, it needs to be reset to 0. You have the tools to do it, so it should be simple to implement.

After you deal with that, do you also deal with the key and the plain text character having different cases - one uppercase and the other lowercase?

If this answers your question, please click 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 .