0

my code for Caesar works fine regarding the ciphering part but it won't pass the check50 because of the criteria "handles non-numeric key" and I have this message : "timed out while waiting for program to exit"

Here is my code

int main(int argc, string argv[])

{

    if (argc == 2)
    {
        //check if key is correct
        for (int i = 0, n = strlen(argv[1]); i < n ; i++)
        {
            if (!isdigit(argv[1][i]))
            {
                printf("Usage: ./caesar key\n");
                return 1;
            }

            else


            {
                int k = atoi(argv[1]);

                //getting plaintext from user
                string p = get_string("plaintext: ");


                //printing cypher text
                {
                    printf("ciphertext: ");
                }

                for (int j = 0, m = strlen(p) ; j < m ; j++)
                {
                    if (isalpha(p[j]))

                        if (isupper(p[j]))
                        {
                            printf("%c", (((p[j] - 65) + k) % 26) + 65);
                        }
                        else
                        {
                            printf("%c", (((p[j] - 97) + k) % 26) + 97);
                        }

                    else
                    {
                        printf("%c", p[j]);
                    }
                }
                printf("\n");
                return 0;

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

My first guess was that my isdigit function was wrong but by checking in this forum it looks fine for me.

So i did a couple tests, I get different error messages for the following character : & " ( ) | ` \ )

I don't understand because those characters are not digits so they should be exluded by the isdigit function right ?

Can someone explain or give me some hint on why it doesn't work ?

Thanks !

2
  • Hint: try with cipher key "2x". isdigit looks right, but study the program to see why only the first char gets tested. – DinoCoderSaurus Apr 7 '20 at 14:35
  • Hi, thanks for the hint, I finally found the solution. All my code was in my first loop ! – Martinf Apr 8 '20 at 10:48

You must log in to answer this question.

Browse other questions tagged .