我想知道是否可以检查字符串中的一个字母是否大写。另一种方式,如果字符串中的所有字母都是大写或小写。示例:
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;
}发布于 2016-11-09 05:05:58
你可以使用标准算法std::all_of
if( std::all_of( str.begin(), str.end(), islower ) ) { // all lowercase
}发布于 2016-11-09 05:05:17
这可以通过lambda表达式轻松完成:
if (std::count_if(a.begin(), b.end(), [](unsigned char ch) { return std::islower(ch); }) == 1) {
// The string has exactly one lowercase character
...
}这里假设您只想检测一个大写/小写字母,如您的示例所示。
发布于 2016-11-09 05:06:15
将all_of与isupper和islower配合使用
if(all_of(a.begin(), a.end(), &::isupper)){ //Cheking if all the string is lowercase
cout << "The string a contain a uppercase letter" << endl;
}
if(all_of(a.begin(), a.end(), &::islower)){ //Checking if all the string is uppercase
cout << "The string b contain a lowercase letter" << endl;
}或者,如果您想检查与谓词匹配的字母数,可以使用count_if。
https://stackoverflow.com/questions/40496334
复制相似问题