我有一个指向 char 数组的指针。我想增加它直到它不指向数字/数字(在 char 数组中表示为 char)。
例如,如果 pointer
指向此 char 数组中的 '2'
:
['1'][' ']['2']['3']['4'][' '][' '][' ']['5']['6']['7']
^
*pointer
我想增加它直到它指向第一个非数字字符 - ' '
:
['1'][' ']['2']['3']['4'][' '][' '][' ']['5']['6']['7']
^
*pointer
我知道我可以做这样的事情:
while (*pointer == '0' || *pointer == '1' || *pointer == '2' || ...)
pointer++;
return pointer;
但是它很长而且一点也不优雅。
我想我可以使用 atoi()
,当指针不指向数字时它返回 0
:
while (atoi(pointer) != 0 || *pointer == '0') //while it still points at a number
pointer++; //increase the pointer until it will not point at a number
return pointer;
但是好像不行。也许没关系,我在其他地方有错误,但无论如何我想知道:
在 C 语言中,是否有任何其他(更好的)方法来检查指向 char 数组的指针是否指向数字/数字并增加它直到它指向非数字字符?
最佳答案
您可能应该改用 ctype.h
中的 isdigit
。像这样的东西:
while (*pointer && isdigit(*pointer))
pointer++;
关于c - 如何检查指向 char 数组的指针是否指向数字字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13659829/