检查字符串是否有大写或小写字母

新手上路,请多包涵

我想知道是否可以检查字符串的一个字母是否大写。其他方式查看它,如果字符串中的所有字母都是大写或小写。例子:

 string a = "aaaaAaa";
string b = "AAAAAa";

if(??){ //Cheking if all the string is lowercase
   cout << "The string a contain a uppercase letter" << endl;
}
if(??){ //Checking if all the string is uppercase
       cout << "The string b contain a lowercase letter" << endl;
}

原文由 user7024664 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.1k
2 个回答

你可以使用标准算法 std::all_of

 if( std::all_of( str.begin(), str.end(), islower ) ) { // all lowercase
}

原文由 Slava 发布,翻译遵循 CC BY-SA 4.0 许可协议

这可以通过 lambda 表达式轻松完成:

 if (std::count_if(a.begin(), b.end(), [](unsigned char ch) { return std::islower(ch); }) == 1) {
    // The string has exactly one lowercase character
    ...
}

根据您的示例,这假设您想要准确检测一个大写/小写字母。

原文由 Sergey Kalinichenko 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
logo
Stack Overflow 翻译
子站问答
访问
宣传栏