0

I am able to execute the code with any errors. The solution(output) to the examples given in the problem set 2 match my solution and yet check50 shows there are a number of issues in the output. Check50 states that the expected output and the output gotten by executing the code are different.

What should I do to overcome this issue?

Code:

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

bool valid(string text);

int main (int argc, string argv[])
{
    if(argc != 2)
    {
        printf("Usage: ./substitution key\n");
        return 1;
    }
    if( !valid(argv[1]))
    {
        printf("Key must contain 26 characters.\n");
        return 1;
    }
    string text = get_string("Plaintext: ");
    printf("Ciphertext: ");
    string alpha="abcdefghijklmnopqrstuvwxyz";
    char citext[strlen(text)];
    for (int i = 0; i < strlen(text); i++)
    {
        if(text[i] >= 'A' && text[i] <= 'Z')
        {
            for (int j = 0; j < 26; j++)
            {
                if(alpha[j] == tolower(text[i]))
                {
                    citext[i] = toupper(argv[1][j]);
                    printf("%c", citext[i]);
                    break;
                }
            }
        }
        else if((text[i] >= 'a' && text[i] <= 'z'))
        {
            for (int j = 0; j < 26; j++)
            {
                if(alpha[j] == text[i])
                {
                    citext[i] = tolower(argv[1][j]);
                    printf("%c", citext[i]);
                    break;
                }
            }
        }
        else
        {
            printf("%c", text[i]);
        }
    }
    printf("\n");
}


bool valid(string text)
{
    if(strlen(text) != 26)
    {
        return false;
    }
    for (int i = 0, n = strlen(text); i < n; i++)
    {
        if (!isalpha(text[i]))
        {
            return false;
        }
        for (int j = 0; j < n; j++)
        {
            if(toupper(text[i]) == toupper(text[j]) && i != j)
            {
                printf("Key must not be repeated.\n");
                return false;
            }
        }
    }
    return true;
}

error message:

:) substitution.c exists
:) substitution.c compiles
:( encrypts "A" as "Z" using ZYXWVUTSRQPONMLKJIHGFEDCBA as key
    expected "ciphertext: Z\...", not "Ciphertext: Z\..."
:( encrypts "a" as "z" using ZYXWVUTSRQPONMLKJIHGFEDCBA as key
    expected "ciphertext: z\...", not "Ciphertext: z\..."
:( encrypts "ABC" as "NJQ" using NJQSUYBRXMOPFTHZVAWCGILKED as key
    expected "ciphertext: NJ...", not "Ciphertext: NJ..."
:( encrypts "XyZ" as "KeD" using NJQSUYBRXMOPFTHZVAWCGILKED as key
    expected "ciphertext: Ke...", not "Ciphertext: Ke..."
:( 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..."
:) handles lack of key
:) handles invalid key length
:) handles invalid characters in key
:) handles duplicate characters in key
:) handles multiple duplicate characters in key

The output I am getting(for examples) is as follows

0

The answer is in plain sight in the check50 results. Simply put, every test that encodes the data has failed for the same reason. Don't even have to look at the code.

This is a good lesson that a program has to be written to EXACTLY match the program specification. Close doesn't get the job done. It has to be an EXACT match to the program spec. The slightest error can cause the program not to work correctly! This code didn't match the spec.

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

BTW, is "Ciphertext" supposed to start with a capital C or a lowercase c ??? ;-)

You must log in to answer this question.

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