C++ 字符类型常用成员函数详解
C++ 字符类型常用的成员函数有:
isalpha():判断字符是否为字母isdigit():判断字符是否为数字isalnum():判断字符是否为字母或数字islower():判断字符是否为小写字母isupper():判断字符是否为大写字母isspace():判断字符是否为空格字符tolower():将字符转换为小写字母toupper():将字符转换为大写字母
示例代码:
#include <iostream>
#include <cctype>
using namespace std;
int main() {
char ch = 'A';
if (isalpha(ch)) {
cout << "'" << ch << "' is an alphabet." << endl;
}
if (isdigit(ch)) {
cout << "'" << ch << "' is a digit." << endl;
}
if (islower(ch)) {
cout << "'" << ch << "' is a lowercase letter." << endl;
}
if (isupper(ch)) {
cout << "'" << ch << "' is an uppercase letter." << endl;
}
if (isspace(ch)) {
cout << "'" << ch << "' is a whitespace character." << endl;
}
cout << "'" << ch << "' converted to lowercase: " << tolower(ch) << endl;
cout << "'" << ch << "' converted to uppercase: " << toupper(ch) << endl;
return 0;
}
输出:
'A' is an alphabet.
'A' is an uppercase letter.
'A' converted to lowercase: a
'A' converted to uppercase: A
原文地址: https://www.cveoy.top/t/topic/pkTW 著作权归作者所有。请勿转载和采集!