0

I am going nuts. I can't figure out the issue. I've added some code to make sure the key letter is the same case as the plain text.

Initially, it was showing cyphertext: CaQGoh Now, it's showing cyphertext: CaQGod

Closer, but still not right

I've checked multiple other threads, but they didn't help. Any ideas?

 #include <stdio.h>
   #include <cs50.h>
   #include <ctype.h>
   #include <string.h>
   #include <stdlib.h>

int main(int argc, string argv[])
{
    if (argc == 2)
    {

        // Convert the string to an array
        string key = argv[1];

        int y = 0;
        // printf("String \"%s\" is %lu characters long.\n", key, strlen(key));

        do
        {
            if isalpha(key[y])
            {
                y++;

            }
            else
            {
                return 1;
                break;
            }
        }
        while (y < strlen(key));


        int convert;

        string entry = get_string("plaintext: ");


        y = 0;
        int x = 0;


        printf("ciphertext: ");
        do
        {
            if isupper(entry[y])
            {
                // Upper case conversion

                convert = (((entry[y] - 65) + (toupper(key[x]-65))) % 26) + 65;

                printf("%c", convert);
                y++;
                x++;
            }
            else
            {
                // Something wrong here
                if islower(entry[y])
                {
                    // Lower case conversion
                    convert = ((entry[y] - 97 + (tolower(key[x] - 97))) % 26) + 97;

                    printf("%c", convert);
                    y++;
                    x++;
                }
                else
                {
                    // Print out non-characters
                    printf("%c", entry[y]);
                    y++;
                }
            }
        if (x > strlen(key)-1)
        {
            x=0;
        }


        }
        while (y < strlen(entry));
        // Add a newline to the end of the cihertext string
        printf("\n");
        // Return a success value to main
        return 0;

    }
    else
    {
        // Error handling
        printf("Incorrect number of arguments");
        return 1;
    }


}
1

Hi welcome to the CS50 stack!

Look carefully at the difference between:
tolower(key[x] - 97) and tolower(key[x]) - 97

When key[x] is Z as in the BaRFoo example, this tolower(key[x] - 97) (as it is written in the code) evaluates to 249. Say what? Mathematically, "key[x] - 97" is -7. But, tolower takes an unsigned char as it's argument, so -7 is "recast" to 249. From man tolower:

RETURN VALUE The value returned is that of the converted letter, or c if the conversion was not possible.

So convert will be 100 when ciphering 'o' with 'Z'.

This same "problem" exists in the isupper branch, but obviously it does not provoke the same problem with the test data. I haven't done the math to figure out what condition will break it.

1
  • YES! YES!!!!! You are my hero!! SIgh - stupid parentheses. I was SO close. Thanks for the help!!
    – Brandy B
    Aug 27 '18 at 23:47

You must log in to answer this question.

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