0

I am working on the main part of the problem. The previous checks all pass ( checking if two arguments, if key is 26 lenght, if key is only alphabetical chars and if every char is only once). But the main part of the substitution algorithm does not work correct. Some phrases pass ok, some false. But even if I repeat maye the same key with the same plaintext, it sometimes puts out the correct ciphertext, sometimes not. I am very confused right now.. Several debug50 checks couldnt help me to find the bug.

enter image description here

This is the part of the code, where i implemented the substitution algorithm:

else
                {
                    string plaintext = get_string("plaintext: ");
                    int charcount = strlen(plaintext);
                    char ciphertext[charcount];
                    string abc = "abcdefghijklmnopqrstuvwxyz";
                    
                    for (int i = 0; i < charcount; i++)
                    {
                        if (isupper(plaintext[i]) != 0)
                        {
                            for (int j = 0; j < 26; j++)
                            {
                                if (abc[j] == tolower(plaintext[i]))
                                {
                                    ciphertext[i] = toupper(key[j]);
                                    break;
                                }
                            }
                        }
    
                        else if (islower(plaintext[i]) != 0 )
                        {
                            for (int k = 0; k < 26; k++)
                            {
                                if (abc[k] == plaintext[i])
                                {
                                    ciphertext[i] = tolower(key[k]);
                                    break;
                                }
                            }
                        }
                        else 
                        {
                            ciphertext[i] = plaintext[i];
                        }
                    }
                    
                    printf("ciphertext: %s\n", ciphertext);
                    return 0;
                }

Expample of the failure:

If I put in the key YTNSHKVEFXRBAUQZCLWDMIPGJO and type as plaintext just an character, in this case:

A

The programm will put out Yu. When i run it with exactly the same parameters, it puts out Y. in the third try again Y. And in the fourth again Yu. This seems very randomly to me. Unfortunatelly I don't find the mistake in the code. What have I done wrong?

0

Try copying your code into another file, compile and run the new one. Or even paste your code into another text editor or IDE which isn't the CS50 IDE and run it and see if the same problem comes up.

Hope it helps.

0

The Problem was, that i used a char array and assigned char(s) from a string to it. See at line 35:

ciphertext[i] = plaintext[i];

This leds to a missing null sign at the end of the string in some cases. I still do not know why this only leds to a problem while using single word or single character as plaintext but not when I used more then one word with a space between the words. But anyway the solution was to add a null char at the end of the char array. In my case I did it with this code:

ciphertext[charcount] = '\0';

You must log in to answer this question.

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