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

7 c++ string lowercase

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

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;
}
Run Code Online (Sandbox Code Playgroud)

谢谢.

Sla*_*ica 12

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

if( std::all_of( str.begin(), str.end(), islower ) { // all lowercase
}
Run Code Online (Sandbox Code Playgroud)

  • `std :: islower`嗯? (2认同)
  • 如果你反转条件:`!std :: any_of(str.begin(),str.end(),isupper)`,你就可以提前返回. (2认同)