简体   繁体   English

在 C 中读取命令行参数并检查它是否为数字

[英]Reading command-line argument in C and checking is it a digit

Details about my goal关于我的目标的详细信息

In C, my program is supposed to take an input from command line, read it, make sure that it is a digit and then print out "Success" if true, "Usage: ./caesar key" if false.在 C 中,我的程序应该从命令行获取输入,读取它,确保它是一个数字,然后如果为真则打印出“成功”,如果为假则打印出“用法:./凯撒密钥”。

Expected results预期成绩

./caesar 33
Success

or或者

./caesar zzz
Usage: ./caesar key

Actual results实际结果

./caesar 33
Segmentation error

What I've tried我试过的

 int main (int argc, string argv[])
 {
   if (argc == 2)
   {
      if (isdigit(argv[1]))
      {
        printf("Success\n");
        return 0;
      }

or或者

if (argc == 2)
{
  if (isdigit(argv[0][1]))
  {
    printf("Success\n");
    return 0;
 }

or或者

int main (int argc, string argv[])
{
   if (argc == 2 && isdigit(argv[1]))
   {
     printf("Success\n");
     return 0;
   }

What you are doing wrong here is that you want to check if the entire string consists of digits, but you are passing the string itself to isdigit , which expects a char .你在这里做错的是你想检查整个字符串是否由数字组成,但是你将字符串本身传递给isdigit ,它需要一个char What you can do is manually iterate through the char array and check if each char is a digit, or you can use the strspn function as demonstrated below.您可以做的是手动遍历char数组并检查每个char是否为数字,或者您可以使用strspn函数,如下所示。

strspn(argv[1], "0123456789") == strlen(argv[1])

This will compare the span of digits in the argument and return true if it is the same as the length of the string itself.这将比较参数中数字的跨度,如果它与字符串本身的长度相同,则返回 true。 If you want to allow + and - signs in the argument, use "-+0123456789" inside strspn , though that would result in a false positive for cases like +++++ (as mentioned by @dreamcrash below), which is why defining a custom function is always a better option.如果您想在参数中允许 + 和 - 符号,请在strspn使用"-+0123456789" ,尽管这会导致+++++情况出现误报(如下面@dreamcrash 所述),这就是为什么定义自定义函数总是更好的选择。 Both the strspn and strlen functions are available in the string.h header. strspnstrlen函数都在string.h头文件中可用。

If you do not need to use the isdigit function, then you can use the strspn function as suggested by @Param Siddharth.如果您不需要使用isdigit函数,那么您可以使用 @Param Siddharth 建议的strspn函数。 However, they are several corner-cases with using但是,它们是使用的几种极端情况

if (strspn(string, "-+0123456789") == strlen(string))

for instance that the values like "-----" would be considered as a valid number .例如,像"-----"这样的值将被视为有效数字

If you want to use the isdigit function than you need to go through all the char s in the String (instead of passing the entire String in one go) and check if each one of them is indeed a digit:如果你想使用isdigit函数,那么你需要遍历String中的所有char (而不是一次性传递整个String )并检查它们中的每一个是否确实是一个数字:

    ...
    int i, is_number = 1;
    for (i = 0; is_number && argv[1][i] != '\0'; i++){
        if(i == 0 && (argv[1][i] == '-' || argv[1][i] == '+')){
           continue;
        }   
        is_number = isdigit(string[i]);
     }
     // It should fail for Strings that are : empty, only "-" or "+"
    if(i == 0 || (i == 1 && !isdigit(argv[1][0])) || !is_number){
        printf("The String is not a Number \n");
        return 1;
     }
     printf("Success\n");
     return 0;
}

I tested with the inputs:我用输入进行了测试:

  • "-", not a number “-”,不是数字
  • "", not a number "",不是数字
  • "-1", number “-1”,数字
  • "+1", number “+1”,数字
  • "+1+1" not a number “+1+1”不是数字
int main (int argc, string argv[])

string data type is not defined in C. Second argument should be char* argv[] . C 中未定义string数据类型。第二个参数应为char* argv[]

Please see the answer : https://stackoverflow.com/questions/14709323/does-c-have-a-string-type#:~:text=There%20is%20no%20string%20type,one%20additional%20zero%20terminating%20character .请查看答案: https : //stackoverflow.com/questions/14709323/does-c-have-a-string-type# :~: text=There%20is%20no%20string%20type,one%20additional%20zero% 20 终止%20个字符

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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