0

how to defend against argv[1] with alphabet input?

I can defend against:

./caesar 1 2 3 and ./caesar -123

BUT NOT ./caesar abcd AND ./caesar 12ab

My code is as follow:

int main(int argc, string argv[])
{
    if (argc != 2)
    {
        printf("Usage : ./caesar key\n");
        return 1;
    }

    int key = atoi(argv[1]);
    if (key < 0 )
    {
        printf("Usage: ./caesar key \n");
        return 1;
    }
    else
    {
        printf("how to defend against alphabet input?\n");
    }

Thank you in advance, any kind of help is highly appreciated.

2 Answers 2

0

You simply need to verify that each of the chars in argv[1] are digits. There's a function for that. Hint, have you thought about how a for loop might be useful here? ;-)

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

0

Look up the CS50 preference or the manual on Google, and find the function you need. Recall the argv[] is an array of string, and string is array of characters. You may want to apply the function on argv[]

You must log in to answer this question.

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