Check if string has letter in uppercase or lowercase
you can use standard algorithm std::all_of
if( std::all_of( str.begin(), str.end(), islower ) { // all lowercase
}
This can be easily done 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 exactly one uppercase/lowercase letter, as per your examples.
Use all_of
in concert 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.