判断大小写字母:使用isalpha()判断字符是否是字母。使用isupper()判断字符是否是大写字母。使用islower()判断字符是否是小写字母。使用isupper()和islower()判断字符串中所有字符是否是大写或小写字母。使用toupper()和小写字母转换大小写。

如何在 C++ 中判断大小写字母
判断单个字符
- isalpha(char c): 检查 c 是否是字母(大小写均可)。如果为真,返回非零值;如果为假,返回零。
- isupper(char c): 检查 c 是否是大写字母。如果为真,返回非零值;如果为假,返回零。
- islower(char c): 检查 c 是否是小写字母。如果为真,返回非零值;如果为假,返回零。
示例:
char c = 'A';
if (isalpha(c)) {
cout << "c 是字母。" << endl;
}
if (isupper(c)) {
cout << "c 是大写字母。" << endl;
}
if (islower(c)) {
cout << "c 是小写字母。" << endl;
}判断字符串
立即学习“C++免费学习笔记(深入)”;
- isupper(const string& str): 检查字符串 str 中的所有字符是否均为大写字母。如果为真,返回 true;如果为假,返回 false。
- islower(const string& str): 检查字符串 str 中的所有字符是否均为小写字母。如果为真,返回 true;如果为假,返回 false。
示例:
string str = "ABCDEF";
if (isupper(str)) {
cout << "str 中的所有字符均为大写字母。" << endl;
}
if (islower(str)) {
cout << "str 中的所有字符均为小写字母。" << endl;
}其他方法
除了这些内置函数外,还可以使用以下方法来判断字符的大小写:
- toupper(char c): 将小写字符转换为大写字符。
- tolower(char c): 将大写字符转换为小写字符。
示例:
char c = 'a'; c = toupper(c); // c 现在为 'A' c = tolower(c); // c 现在为 'a'











