0

I am new to programming and I am stuck with Pset2 for a week as it does not seem to do the right thing no matter how I modify it. Can you please give me a hint of what's wrong with the code?

     int main(int argc, string argv[])
     {
        // check for 2 arguments only
        if (argc != 2)
    {
        printf("Enter the key please \n");
        return 1;
    }

    // check for correct argv put key into k
    string key = argv[1];

    // check key is text
    for (int j=0, n=strlen(key); j<n; j++)
    {
        if (!isalpha(key[j]))
        printf("Enter only alphabetical key.");
    }


    {
        // prompt user for a code to encrypt
        string msg = get_string("cipher text: ");

        //obtain length of key and turn it to lower case integer
        int kLength = strlen(key);


        for (int i = 0, j = 0, n = strlen(msg); i < n; i++)
            {

                //check if the letter is uppercase or lowercase then convert, use "-97" to turn key ASCII to alphabetical order

                if islower(msg[i])
                {
                    printf("%c", ((msg[i] +tolower(key[j]) - 97- 97) % kLength) + 97);
                    j++;
                }

                else if isupper(msg[i])
                {
                    printf("%c", ((msg[i] + tolower(key[j]) - 65- 97) % kLength) + 65);
                    j++;
                }
                //increment j only if used

                else

                //if neither then just print whatever it is
                    printf("%c", msg[i]);

            }
            printf("\n");
            return 0;
    }
 }

1 Answer 1

0
printf("%c", ((msg[i] +tolower(key[j]) - 97- 97) % kLength) + 97);

So, why is the length of the key being applied to the encrypted value of the plaintext using % ???

Think about exactly what the length of the key should be applied to using % !!!

There are other problems too, but they're more minor and you should be able to find them easily enough.

If this is the hint you wanted, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

1

You must log in to answer this question.

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