2

I have finished the substitution pset2 problem, however, there was one error when I submitted my assignment. The description of this error was handles multiple duplicate characters in key. timed out while waiting for program to exit. I am not sure if I understand this error since I have used a key with multiple duplicate and ./substitution seems to work and provide an appropriate response. As an example, I used ./substitution BBCCEFGHIJKLMNOPQRSTUVWXYZ. In this example, I use the plaintext: "bilal" and the outcome was ciphertext: bilbl, which I think is the correct answer here given the key.

The following is my code

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

    //Function decleration - validate_key_number_of_digits
    bool validate_key_number_of_digits(char key[]);


    //Function decleration - validate_key_all_alpha
    bool validate_key_all_alpha(char key[]);


    //Function decleration - substitute
    char substitution (char alpha_key[]);

    //Main function
    int main(int argc, string argv[])
    {
        if(argc != 2)
        {
            printf("Usage: ./substitution key\n");
            return 1;
        }
        int k = strlen(argv[1]);
        if(k != 26)
        {
            printf("Usage: ./substitution key\n");
            return 1;
        }
        if(validate_key_number_of_digits (argv[1]) == false)
        {
            printf("Usage: ./substitution key\n");
            return 1;
        }

        if(validate_key_all_alpha (argv[1]) == false)
        {
            printf("Usage: ./substitution key\n");
            return 1;
        }
        else
        {

            substitution(argv[1]);
            printf("\n");
            return 0;
        }
}



    //validate_key_number_of_digits function
    bool validate_key_number_of_digits(char key[])
    {
        int alpha = 0;

        int n = strlen(key);
        for (int i = 0; i < n; i++)
        {
            if(isalpha(key[i]) != 0)
            {
                alpha++;
            }
        }
        if(alpha == 26)
        {

        return true;
        }
        else
        {
        return false;
        }
    }


    //validate validate_key_all_alpha function
    bool validate_key_all_alpha(char key[])
    {
        int ascii_val = 0;
        int n = strlen(key);
        for (int i = 0; i < n; i++)
        {
            if(islower(key[i]) != 0)
            {
                int x = toupper(key[i]);
                ascii_val += x;
            }
            else
            {
                ascii_val += (int) key[i];
            }
        }

        if(ascii_val != 2015)
        {
            return false;
        }
        else
        {
            return true;
        }
    }


    char substitution (char alpha_key[])
    {

        string plaintext = get_string("Plaintext:");
        int n = strlen(plaintext);

        //int k = 0;
        printf("ciphertext: ");
        for (int i = 0; i < n; i++)
        {

            //If its not an letter, then simply print it as is
            if(isalpha(plaintext[i]) == 0)
            {
                printf("%c", plaintext[i]);
            }

            //checks if the char is a lower case or not. If it is then it executes the code within the {} brackets
            else if(isupper(plaintext[i]) == 0)
            {
            int x = (plaintext[i] % 97);

            //char alpha_key[x];
            if(islower(alpha_key[x]) == 0)
            {
            printf("%c", alpha_key[x] + 32);
            }
            else
            {
            printf("%c", alpha_key[x]);
            }
            }

            //checks if the char is a upper case or not. If it is then it executes the code within the {} brackets
            else if(islower(plaintext[i]) == 0)
            {
            int x = (plaintext[i] % 65);

            if(isupper(alpha_key[x]) == 0)
            {
            printf("%c", alpha_key[x] - 32);
            }
            else
            {
            printf("%c", alpha_key[x]);
            }
            }


        }
        return 0;
    }

Any idea on where I am going wrong with this?

1 Answer 1

4

The problem is this cipher key BBCCEFGHIJKLMNOPQRSTUVWXYZ should not perform the encoding, it should give an error. From the spec [emphasis added]:

If the key is invalid (as by not containing 26 characters, containing any character that is not an alphabetic character, or not containing each letter exactly once), your program should print an error message of your choice (with printf) and return from main a value of 1 immediately.

If a key has duplicates (and is the correct length), it cannot contain each letter exactly once.

1
  • I had written the validate_key_all_alpha function to catch these instances. I will have to review this function and make the required updated. Thank you for your response Dino. Commented Mar 28, 2020 at 23:30

You must log in to answer this question.

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