简体   繁体   English

使用C检查用户输入是否为有效字符串

[英]Check whether user input is a valid string or not using C

I am taking a string input from user. 我正在从用户那里获取字符串输入。 But how d I check whether user has entered a string or a number?? 但是我如何检查用户是否输入了字符串或数字?

Call strtol, check that the value stored to endptr is not equal to the input (successful conversion), and is a pointer to a NUL byte (the whole string was used). 调用strtol,检查存储到endptr的值是否不等于输入(成功转换),并且是指向NUL字节的指针(使用了整个字符串)。

http://www.opengroup.org/onlinepubs/000095399/functions/strtol.html http://www.opengroup.org/onlinepubs/000095399/functions/strtol.html

explains that if you also want to detect overflow, the trick is to set errno to 0, then call strtol, then check that errno is still 0. 解释说,如果你还想检测溢出,诀窍是将errno设置为0,然后调用strtol,然后检查errno是否仍为0。

If you want to be pedantic, you also have to check using isspace() that the first character of the input string is a non-space. 如果你想要迂腐,你还必须使用isspace()检查输入字符串的第一个字符是非空格。 The reason is that strtol and friends skip over initial whitespace, but perhaps you don't consider " 1" to be a valid number. 原因是strtol和朋友跳过了初始空格,但也许你不认为“1”是有效数字。

Alternatives include strtoll and strtod, respectively if you want to allow bigger numbers, or non-integer numbers. 如果您想允许更大的数字或非整数,则替代方案分别包括strtoll和strtod。

You can check if its a digit: 你可以检查它是否是一个数字:

char c;
scanf( "%c", &c );
if( isdigit(c) )
  printf( "You entered the digit %c\n", c );
int isNum;
 if ((isNum = strtol(string, NULL, 10)))
  printf(" '%d' numbers was found!\n", isNum);
else
 {
     printf("No number found");
 }

All valid chars read are converted, if the string starts with an invalid character the function returns ZERO (0). 读取的所有有效字符都将被转换,如果字符串以无效字符开头,则函数返回ZERO(0)。

strtol() example that shows how to print out following characters after numbers or the part of the string thats not numbers. strtol()示例 ,显示如何在数字后打印出后面的字符或字符串中不是数字的部分。

or 要么

 int isNum
 isNum = atoi(string);

Lots of good heuristics here already. 这里有很多好的启发式方法。

If you want to define the input closely, you may want to write your own lexer/parser to accept exactly the language you mean rather that trying to build a makeshift out of standard library functions. 如果你想要密切定义输入,你可能想要编写自己的词法分析器/解析器来接受你所说的语言而不是试图建立一个临时的标准库函数。

This is a substantial topic in and of itself. 这本身就是一个重要的话题。 See one of the may "How to build a compiler?" 请参阅“如何构建编译器”之一? questions on StackOverflow for references...(eg Learning to write a compiler ). 关于参考的StackOverflow的问题......(例如学习编写编译器 )。

You could use atoi or atof and then check if the value returned by this function is not 0. This is not infallible as the user could have entered '0'. 您可以使用atoi或atof,然后检查此函数返回的值是否不为0.这不是绝对可靠的,因为用户可能已输入“0”。

Another way would be to check that every character in the string is either [0-9], +/- or the decimal point and reject every string that doesn't conform. 另一种方法是检查字符串中的每个字符是[0-9],+ / - 还是小数点,并拒绝每个不符合的字符串。

You have not specified what 'numbers' you expect. 您没有指定您期望的“数字”。 Depending on the valid set of inputs, you can call a number of standard library functions as others have specified. 根据有效的输入集,您可以调用许多标准库函数,就像其他人指定的那样。 For example: A simple check if it is a digit does not work if you are expecting floating point numbers. 例如:如果您期望浮点数,则简单检查它是否为数字不起作用。 Also note that it is not enough to call the library functions but you also need to check for the return values (ie errors if any that occurred during the conversion). 另请注意,调用库函数是不够的,但您还需要检查返回值(即转换期间发生的错误)。

Alternatively, you can use the sscanf function to retrieve the numbers. 或者,您可以使用sscanf函数来检索数字。

Finally, an integer can be stored either as a int or variants thereof ( long etc) or a floating point number. 最后,整数可以存储为int或其变体( long等)或浮点数。 The call is yours. 电话是你的。

Instead of using functions like strtol, atoi, atof .. You could define your own function which has parses character by character of the string and builds the output integer. 而不是使用像strtol,atoi,atof这样的函数。你可以定义自己的函数,它按字符串的字符解析并构建输出整数。 In case during this parsing if a character is a non-digit then you can safely say that the string in NaN. 如果在解析过程中如果一个字符是非数字,那么你可以安全地说出NaN中的字符串。 Or else you could also use isdigit() and check for each character and if all the characters are digits then convert the string to number using atoi. 或者您也可以使用isdigit()并检查每个字符,如果所有字符都是数字,则使用atoi将字符串转换为数字。

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

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