【c/c++】isdigit()函数
isdigit函数
isdigit是计算机C(C++)语言中的一个函数,主要用于检查其参数是否为十进制数字字符。
函数定义:int isdigit(int c)
函数说明:检查参数是否为十进制数字字符(判断括号内是否为0~9的数字。)
函数所需头文件:#include<cctype>
返回值:若参数c为阿拉伯数字0~9,则返回非0值,否则返回0。
代码示例
//isdigit:判断字符是否为阿拉伯数字0~9
#include<iostream>
#include<cctype>
using namespace std;
int main()
{
string str = "123456789asdfghjkl~!@#$%";
for(int i = 0; str[i] != 0; ++i)
{
if(isdigit(str[i]))
cout << str[i] << " is digit" << endl;
else
cout << str[i] << " is not digit!" << endl;
}
return 0;
}
打印输出
标准输出:0 is digit
1 is digit
2 is digit
3 is digit
4 is digit
5 is digit
6 is digit
7 is digit
8 is digit
9 is digit
a is not digit!
s is not digit!
d is not digit!
f is not digit!
g is not digit!
h is not digit!
j is not digit!
k is not digit!
l is not digit!
~ is not digit!
! is not digit!
@ is not digit!
# is not digit!
$ is not digit!
% is not digit!
引经据典
https://baike.baidu.com/item/isdigit/9455880?fr=aladdin