C++ 字符类型常用的成员函数有:

  1. isalpha():判断字符是否为字母
  2. isdigit():判断字符是否为数字
  3. isalnum():判断字符是否为字母或数字
  4. islower():判断字符是否为小写字母
  5. isupper():判断字符是否为大写字母
  6. isspace():判断字符是否为空格字符
  7. tolower():将字符转换为小写字母
  8. 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 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录