Check if the string is uppercase or lowercase
all_of is C++ 20's feature.
#include <iostream> #include <algorithm> bool areUpperCase (std::string& str) { if (std::all_of(str.begin(), str.end(), [](char c) { return isupper(c);})) { return true; } return false; } int main() { std::string str = "shirley"; if (std::all_of(str.begin(), str.end(), [](char c) { return islower(c);})) { std::cout << "String " << str << " is lower" << std::endl; } else { std::cout << "Not all characters are lowercase\n";} str = "ROSE"; if (areUpperCase(str)) { std::cout << "String " << str << " is upper" << std::endl; } else { std::cout << "Not all characters are uppercase\n";} return 0; }
Click here to debug.
Convert the string to uppercase or lowercase
#include <iostream> #include <algorithm> int main() { std::string str = "shIrley"; std::for_each(str.begin(), str.end(), [](char & c){ c = ::tolower(c); }); std::cout << str << std::endl; std::string str2 = "yeLRIhs"; std::for_each(str2.begin(), str2.end(), [](char & c){ c = ::toupper(c); }); std::cout << str2 << std::endl; return 0; }
Click here to debug.
Comments
Post a Comment