0

Of the 11 Check50 checks, only 4 check out correct but when I run the code with the Check50 inputs, my outputs match the expected outputs, so why so many errors?

Here is a link to my Check50 results: https://submit.cs50.io/check50/614518398d0d5d4f2db06c097b8c86091a6ad9db

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

int main(int argc, string argv[])
{
    if (argc == 2)
        {
            int key_len = strlen(argv[1]);
            for (int i = 0; i < key_len; i++)
            {
                if (isdigit(argv[1][i]))
                {
                    i++;
                }
                else
                {
                    printf("Usage: ./caesar key\n");
                    return (1);
                }
            }
        int key = atoi(argv[1]);


        string text = get_string("plaintext:  ");
        int j;

        int text_length = strlen(text);
        char new_text[text_length];

        printf("ciphertext: ");
        for (j = 0; j <= text_length; j++)
        {
            if (isupper(text[j]))
            {
                char upper_index = (text[j] - 'A');
                char new_upper = ((upper_index + key) % 26);
                printf("%c", new_upper + 'A');
            }
            else if (islower(text[j]))
            {
                char lower_index = (text[j] - 'a');
                char new_lower = ((lower_index + key) % 26);
                printf("%c", (new_lower + 'a'));
            }
            else
            {
                printf("%c", text[j]);
            }
        }
        printf("\n");
        return (0);

    }
    else
        {
        printf("Usage: ./caesar key\n");
        return (1);
        }

}
1

You can't see it in your output, but you're actually printing one character beyond the end of the string, which is the null terminating character.

printf("ciphertext: ");
for (j = 0; j <= text_length; j++)

In the loop statement here, you're going through j = text_length, but text[text_length] is the null terminating character.

:( handles non-numeric key: this test failure is a separate issue. Try using the input they describe in that test, and keeping track of where i changes to find the cause of that one.

2
  • right on target.
    – Cliff B
    Jul 13 '20 at 23:54
  • Sure thing, that worked! Many thanks!
    – RM00
    Aug 3 '20 at 12:10
0

i am not sure but it could be because of some space error in your code after plaintext or ciphertext. same thing happened to me. The only reason was that i had not put space after "ciphertext:".. get_string("plaintext: "); here there seem to be double space.. Try editing that once.

0

try

for (j = 0; j < text_length; j++)

You must log in to answer this question.

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