cstringprintfcs50substitution

CS50 - Pset 2 - Substitution - Issue with Check50 Not Detecting Output


I'm working on the CS50x 2024 Substitution problem set, and I'm encountering an issue with Check50 and Submit50 not detecting the output from my formatted string. I can see the correct output when I run the program, but the checkers aren't recognizing it.

Here is a snippet of my printf code:

    printf("ciphertext: %s\n", substitution_cipher(plain_text, argv[1]));

Output on My Terminal:

substitution/ $ ./substitution YUKFRNLBAVMWZTEOGXHCIPJSQD
plaintext: This is CS50
ciphertext: Cbah ah KH50
substitution/ $ ./substitution yukfrnlbavmwzteogxhcipjsqd
plaintext: This is CS50
ciphertext: Cbah ah KH50
substitution/ $ ./substitution DWUSXNPQKEGCZFJBTLYROHIAVM
plaintext: The quick brown fox jumps over the lazy dog
ciphertext: Rqx tokug wljif nja eozby jhxl rqx cdmv sjp

Steps I've Tried:

Screenshots:

Is there something wrong with my implementation or an issue with the checker? Any help would be appreciated. Thanks!


Solution

  • As you will learn in week 4 of CS50, the CS50 data type string is nothing else than a reference (called a "pointer") to a char array. Therefore, the string data type does not store any characters itself.

    My guess is that your function substitution_cipher looks somewhat like this:

    string substitution_cipher(string plain_text, string key)
    {
        char cipher_text[strlen(plain_text)+1];
    
        // calculate and write to cipher_text array (code omitted)
    
        return cipher_text;
    }
    

    If this is indeed what your function looks like, then the problem is that the array ciphertext will cease to exist as soon as the function ends. Therefore, there is no point in returning a reference to this array, because you are returning a reference to something that no longer exists, making the reference effectively useless. Although the reference may still work, because the memory location in which the array existed may not have been overwritten with anything else yet, you cannot rely on this.

    The easiest way to fix this problem is to not attempt to store the entire ciphertext in an array, but rather to simply print each individual ciphertext character immediately after calculating it.

    If you really want to store the entire ciphertext in an array, then one thing you can do is to declare the array in the calling function instead of inside the function substitution_cipher, so that the array will continue to exist when the function returns. The calling function can then pass a string reference to this array to the function substitution_cipher. In order to do this, you will have to change the function substitution_cipher like this:

    void substitution_cipher(string cipher_text, string plain_text, string key)
    {
        // calculate and write to cipher_text array (code omitted)
    }
    

    The function substitution_cipher can then be called like this:

    char cipher_text[strlen(plain_text)+1];
    substitution_cipher(cipher_text, plain_text, argv[1]);
    printf("ciphertext: %s\n", cipher_text);