Check if the string has an uppercase or lowercase letter

I would like to know if it is possible to check if one letter of the string is uppercase. Another way to see this is if all the letters in the string are uppercase or lowercase. Example:

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; } 
+7
source share
3 answers

you can use the standard std::all_of

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

This is easy to do with lambda expressions:

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

This assumes that you want to detect only one uppercase / lowercase letter, according to your examples.

+4
source

Use all_of with isupper and 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; } 

demo

Alternatively, use count_if if you want to check the number of letters matching your predicate.

+4
source

Source: https://habr.com/ru/post/1259477/


All Articles