Check if string has letter in uppercase or lowercase

送分小仙女□ 提交于 2019-12-01 16:47:29

you can use standard algorithm std::all_of

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

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!