跳到主要内容

C++ isupper() 判断字符是否为大写字母

isupper() 函数原型

int isupper(int ch);

isupper() 函数用于检查 ch 是否为大写字母,这是基于当前的 C 语言区域设置。默认情况下,从 A 到 Z 的字符(ASCII 值 65 到 90)被视为大写字母。

如果 ch 的值无法表示为无符号字符或不等于 EOF,则 isupper() 的行为是未定义的。

它定义在 <cctype> 头文件中。

isupper() 参数

ch:要检查的字符。

isupper() 返回值

如果 ch 为大写字母,isupper() 函数返回非零值;否则返回零。

示例:isupper() 函数如何工作

#include <cctype>
#include <iostream>
#include <cstring>

using namespace std;

int main()
{
char str[] = "This Program Converts ALL UPPERCASE Characters to LOWERCASE";

for (int i = 0; i < strlen(str); i++)
{
if (isupper(str[i]))
/* 将大写字符转换为小写字符 */
str[i] = str[i] + 32;
}

cout << str;
return 0;
}

当你运行这个程序时,输出将会是:

this program converts all uppercase characters to lowercase