0

I solved the Caesar problem and it works flawlessly. But I can not check it properly. The results are correct but ''' check50 cs50/problems/2021/x/caesar ''' says it is incorrect. If I view the results everything looks correct but just doesn't get accepted by check50. I am sorry if this is a frequently asked question but I worked so many hours for these few lines of code...

My Code below:

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

//initialize vars
int i;
int n;
int pw;
string key;
int indexnumber;

// Initialize alphabet to calculate indexes
char alphalower[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
char alphaupper[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};

int main(int argc, string argv[])
{
    //checks input
    if (argc != 2)
    {
        printf("Usage: ./ceasar key\n");
        return 1;
    }
    key = argv[1];
    
    //checks for non digits
    for (i = 0, n = strlen(key); i <= n; i++)
    {
        if (isalpha(argv[1][i])) 
        {
            printf("Usage: ./caesar key\n");
            return 1;
        }
    }




    string pltext = get_string("plaintext: ");
    
    n = strlen(pltext);

    
    char *cipher;
    cipher = (char *)malloc(n * sizeof(char)); //creating the array dynamically


    //Converts string to int
    for (i = 0, n = strlen(key); i <= n; i++)
    {
        pw = atoi(key);
    }



    printf("ciphertext: ");

    for (i = 0, n = strlen(pltext); i <=  n; i++)
    {
        //goes through every letter and encrypts
        if (isupper(pltext[i]))
        {
            indexnumber = (((pltext[i] % 'A') + pw) % 26);
            cipher[i] = alphaupper[indexnumber];
            printf("%c", cipher[i]);
        }
        else if (islower(pltext[i]))
        {
            indexnumber = (((pltext[i] % 'a') + pw) % 26); // B
            cipher[i] = alphalower[indexnumber];
            printf("%c", cipher[i]);
        }
        else if (isalpha(pltext[i]) == 0)
        {
            printf("%c", pltext[i]);
        }

    }
    printf("\n");
    return 0;


}

And here the check50 output:

~/pset2/ceasar/ $ check50 cs50/problems/2021/x/caesar
Connecting......
Authenticating.....
Verifying.......
Preparing................
Uploading...............
Waiting for results........................................
Results for cs50/problems/2021/x/caesar generated by check50 v3.1.2
:) caesar.c exists.
:) caesar.c compiles.
:( encrypts "a" as "b" using 1 as key
    expected "ciphertext: b\...", not "ciphertext: b\..."
:( encrypts "barfoo" as "yxocll" using 23 as key
    expected "ciphertext: yx...", not "ciphertext: yx..."
:( encrypts "BARFOO" as "EDUIRR" using 3 as key
    expected "ciphertext: ED...", not "ciphertext: ED..."
:( encrypts "BaRFoo" as "FeVJss" using 4 as key
    expected "ciphertext: Fe...", not "ciphertext: Fe..."
:( encrypts "barfoo" as "onesbb" using 65 as key
    expected "ciphertext: on...", not "ciphertext: on..."
:( encrypts "world, say hello!" as "iadxp, emk tqxxa!" using 12 as key
    expected "ciphertext: ia...", not "ciphertext: ia..."
:) handles lack of argv[1]

To see the results by yourself go here

https://submit.cs50.io/check50/98cd213123f8b4eb8f76bc6837ad2e3d260fdb02

I just don't know what to do. Thanks in advance ;)

1 Answer 1

1

Look at the for loop:

for (i = 0, n = strlen(pltext); i <=  n; i++)

Note that it allows i == strlen(plaintext). Bottom line is that the code is processing one char past the end of cipher. Exactly what is printed is unpredictable, but the odds are that it will be a non-printable character. Check50 can detect these non-printables. That's why it's failing.

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

1
  • Hey I was having the same problem and your answer was incredibly (and instantly) helpful! Thank you so much! :)
    – Cecilia
    Commented Aug 28, 2022 at 17:57

You must log in to answer this question.

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