0

hey i think my code is correct, ive compiled it and tested it. check50 is not passing it. the error messages and my code is below. can someone please help?

  • :( encrypts "This is CS50" as "Cbah ah KH50" using YUKFRNLBAVMWZTEOGXHCIPJSQD as key expected "ciphertext: Cb...", not "ciphertext: Cb..."

  • :( encrypts "This is CS50" as "Cbah ah KH50" using yukfrnlbavmwzteogxhcipjsqd as key expected "ciphertext: Cb...", not "ciphertext: Cb..."

  • :( encrypts "This is CS50" as "Cbah ah KH50" using YUKFRNLBAVMWZteogxhcipjsqd as key expected "ciphertext: Cb...", not "ciphertext: Cb..."

  • :( encrypts all alphabetic characters using DWUSXNPQKEGCZFJBTLYROHIAVM as key expected "ciphertext: Rq...", not "ciphertext: Rq..."

  • :( does not encrypt non-alphabetical characters using DWUSXNPQKEGCZFJBTLYROHIAVM as key expected "ciphertext: Yq...", not "ciphertext: Yq..."

  • :( handles duplicate characters in key timed out while waiting for program to exit

  • :( handles multiple duplicate characters in key timed out while waiting for program to exit

#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(int argc, string argv[])
{
    // ascertain there is one key
    if (argc != 2)
    {
        printf("Usage: ./substitution key\n");
        return 1;
    }

    // ascertain key is valid
    int keycount = 0;
    for (int i = 0, j = strlen(argv[1]); i < j; i++)
    {
        keycount++;
        if (!isalpha(argv[1][i]))
        {
            printf("Key must contain 26 letters\n");
            return 1;
        }
    }
    if (keycount != 26)
    {
        printf("Key must contain 26 letters\n");
        return 1;
    }

    // get plaintext
    string text = get_string("plaintext:");

    // encode text
    char ct[26];
    for (int i = 0, j = strlen(text); i < j; i++)
    {
        if (isupper(text[i]))
        {
            ct[i] = toupper(argv[1][text[i] - 'A']);
        }
        else if (islower(text[i]))
        {
            ct[i] = tolower(argv[1][text[i] - 'a']);
        }
    }

    //print ciphertext
    printf("ciphertext: %s\n", ct);

    return 0;
}

1 Answer 1

1

There appear to be several problems with the code. Check the website address generated by check50 for details.

It fails to copy any non-alpha characters to the output array ct[]. Since those char positions are left with '\0' in them, the first non-alpha position is detected as the end of string marker by the system when printing out the array.

The code sets the length of ct[] to an arbitrary length of 26. It should be the length of the plaintext that is entered.

There's no code to check for duplicate letters in the key.

There may be other issues, but this should keep you busy.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

2
  • thanks for the answer. i dont understand what it means it doesnt handle duplicate characters in key. when i check it myself it works?
    – yosef
    Aug 26, 2022 at 16:38
  • I'm thinking that when you tried to test for duplicate letters in the key, your test key actually triggered a different error and not the dupe letter test. If you run ./substitution YFDTSMPBVIEERGHWONUAKLQXCZ it should fail because of the EE in the middle of the key. Instead, it prompts for input from the user. When a check50 test fails, go to the website in the output to see more details and to get the test data that was used (in most cases.)
    – Cliff B
    Aug 26, 2022 at 17:29

You must log in to answer this question.

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