简体   繁体   中英

How do I Validate a Key in ARGV?

I am so stuck. Been trying for days.I need to validate that each char input into argv[1] is a number but I have tried isdigit , isalpha , char , atoi conversion without any success. I'm still new at coding. I got frustrated and deleted everything to start over again but here's where I'm at now.

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

int main(int argc, string argv[])
{
    if (argc == 2)        
    {
        for (int k = 0; k <strlen(argv[1]); k++)
        {
            printf("Success!\n");
            printf("%s\n", argv[1]);
        }    
    }     
    else                  
    {
        printf("Usage: ./caesar key\n");
    }
}

Here is where I was at before. I kept getting a sanitization error everytime I tried to run this:

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

int main(int argc, string argv[])
{
    if (isdigit(argv[1]))
    {
        if(atoi(argv[1])) 
        {
            if (argc == 2)
            {         
                for (int x = 0; x < strlen(argv[1]); x++)
                {
                    printf("Success!\n");
                    printf("%s", argv[1]);
                }
            }        
        }
    }
    else
    {
        printf("Usage: /caesar key\n");
    }
}

Any help would be appreciated!

A quick and dirty example on onlinegdb :

Didn't test a lot...

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

int main(int argc, char * argv[])
{
    if (argc == 2)        
    {
        int n = strlen(argv[1]);
        for (int k = 0; k < n; k++)
        {
            if ( isdigit(argv[1][k]) )
            {
                printf("%c is a digit\n", argv[1][k]);
            }
            else
            {
                printf("%c is not a digit\n", argv[1][k]);
            }
        }    
    }     
    else                  
    {
         printf("Error\n");
    }
}

Remember that argv[1] is a string, ie. an array of characters (with a null chracter at the end).

So you have to travel all the characters of this array.

EDIT: good comment from chmike in the other answer.

You had a good start with for (int x = 0; x < strlen(argv[1]); x++) , unfortunately it is not appropriate for C.

In C a string is terminated by the special character '\\0'. The function strlen counts the characters from the start of the string to this special character. So when you write for (int x = 0; x < strlen(argv[1]); x++) is executes strlen() at each test of x .

Here is one way to avoid this:

 for (int x = 0, n = strlen(argv[1]); x < n; x++)

This declares i initialized with 0, and n initialized with the length of argv[1]. The test compares x with n which is fast.

Another way to test if the end of the string is reached is to test if we reached that special character '\\0'.

 for (int x = 0; argv[1][x] != '\0'; x++)

They are both equivalent. Make your pick.

Now, for each character you want to test if its a digit. You could use the function isdigit() that does the job for you. Or you could write the test yourself which is not that difficult.

for (int x = 0, n = strlen(argv[1]); x < n; x++)
    if (!isdigit(argv[1][x])) {
        printf("failure: argv[1] is not all digits\n");
        return;
    }
printf("success: argv[1] is all digits\n")

The alternative test implemented ourselves is this

for (int x = 0, n = strlen(argv[1]); x < n; x++)
    if (argv[1][x] < '0' || argv[1][x] > '9') {
        printf("failure: argv[1] is not all digits\n");
        return;
    }
printf("success: argv[1] is all digits\n")

Note that argv[1][x] contains the ASCII code of the character and that '0' is the ASCII code of 0. Note also that the ASCII code of digits is in the range '0' to '9' and this range contains only digits. This is why the test above test if argv[1][x] is not a digit.

You could use some helper functions to check whether a string consists of all digits.

/* Return pointer to first non-digit character in a string. */
const char *findnondigit(const char *input)
{
    while (*input >= '0' && *input <= '9')
    {
        input++;
    }
    return input;
}

/* Return 1 if string is all digits, or 0 if it contains a non-digit. */
int checkalldigits(const char *input)
{
    /* Find first non-digit character in input. */
    input = findnondigit(input);
    /* Return whether first non-digit character is the string's null terminator. */
    return *input == '\0';
}

For example:

int main(int argc, char *argv[])
{
    if (argc > 1)
    {
        if (checkalldigits(argv[1]))
        {
            printf("%s is all digits\n", argv[1]);
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM