0

I hope you are all well. Just had a quick question about CS50 Pset2's substituion problem.

Please see my code below.

#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
    {

        printf("ciphertext: %c", substitution(argv[1]));
        //substitution(argv[1]);
        //printf("ciphertext:");
        return 0;
    }
        printf("\n");
}



//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]) == true)
        {
            int x = toupper(key[i]);
            ascii_val += x;
        }
        else
        {
            ascii_val += (int) key[i];
        }
    }

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


char substitution (char alpha_key[])
{
    string plaintext = get_string("Plaintext:");
    int n = strlen(plaintext);

    //int k = 0;
    for (int i = 0; i < n; i++)
    {
        if(isupper(plaintext[i]) == 0)
        {
        int x = (plaintext[i] % 97);
        printf("%c", alpha_key[x]);
        }
        if(islower(plaintext[i]) == 0)
        {
        int x = (plaintext[i] % 65);
        printf("%c", alpha_key[x]);
        }
    }
    return 0;
}

The following are my questions: 1. On the 2nd last line, I had to put in return 0; to avoid having an error come up while compiling. The error said "reaching end of non-void function". I am not sure why I am getting this error, since: i) My if statements encompass all possible outcomes - so whatever argument this substitution function gets, it will be encompassed within the if statements. ii) Since the return type of substitution is a char, wouldn't it be weird that I am returning 0 when that is an int?

  1. The expected output per this pset should be in the format of ciphertext: the actual ciphertext. However, based on my code below, the result is coming out to the actual ciphertext ciphertext: I have tried multiple different ways, but still don't come up with a way to ensure that the output is in the format requested. Can you please assist.
0

I believe that the answer to the first problem is also the same issue with the second problem.

You said that you added return 0; to resolve the error and that you had covered all possible outcomes with the substitution() function. In truth, it doesn't cover all possible outcomes and it doesn't return anything other than 0 at the end.

Look at the code for the substitution() function. It is required to return a char. In reality, it's printing a char, but not returning anything in the if code blocks. The code always runs until it hits the return 0; statement and it's always returning 0.

That explains the first error.

Now, the second error. The code appears to be printing the 'encoded' text inside the substitution() function, but returning 0. The main program is calling the substitution() function in the printf statement. But, in reality, it's executing the printf statements inside the substitution function before executing the printf in main, so the "ciphertext:" is being printed last.

There are other issues in the code that you need to work through, so I'll leave it at that and give you a chance to work on it yourself.

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

| improve this answer | |

You must log in to answer this question.

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