C++ tolower() 将字符转换为小写
C++ 中的 tolower()
函数用于将给定字符转换为小写。它定义在 cctype 头文件中。
示例
#include <iostream>
#include <cctype>
using namespace std;
int main() {
// 将 'A' 转换为小写
char ch = tolower('A');
cout << ch;
return 0;
}
// 输出:a
tolower() 语法
tolower()
函数的语法是:
tolower(int ch);
tolower() 参数
tolower()
函数接受以下参数:
- ch - 一个字符,转换为
int
类型或EOF
tolower() 返回值
tolower()
函数返回:
- 对于字母 -
ch
的小写版本的 ASCII 码 - 对于非字母 -
ch
的 ASCII 码
tolower() 原型
在 cctype 头文件中定义的 tolower()
函数原型是:
int tolower(int ch);
可以看到,字符参数 ch
被转换为 int
,即其 ASCII 码。
由于返回类型也是 int
,tolower()
返回转换后字符的 ASCII 码。
tolower() 未定义行为
tolower()
的行为在以下情况下是未定义的:
ch
的值无法表示为unsigned char
,或ch
的值不等于EOF