isdigit
isdigit
是 C++ 中的一个函数,用于检查一个字符是否为数字字符。这个函数在 <cctype>
头文件中声明。
这个函数的原型是:
int isdigit(int c);
它接受一个字符作为参数,如果这个字符是数字字符(0-9),则返回非零值(通常是1),否则返回0。
例如:
#include <iostream>
#include <cctype>
using namespace std;
int main() {
char c = '7';
if (isdigit(c)) {
cout << c << " is a digit." << endl;
} else {
cout << c << " is not a digit." << endl;
}
return 0;
}
这段代码会输出:7 is a digit.
因为字符 ‘7’ 是数字字符。
另外,isdigit
函数可以用于判断一个字符是否为数字,但是要注意它只能检查单个字符,不能判断整个字符串是否为数字。
isalnum
isalnum
函数是 C++ 标准库中的一个函数,用于检查字符是否为字母或数字。它是 <cctype>
头文件中的一部分。isalnum
函数对所有大写和小写字母以及数字返回 true
,对其他字符返回 false
。
#include <cctype>
int isalnum(int ch);