std::string is uppercase or lowercase in C++

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;
}

Output:

1
2
3
String shirley is lower
String ROSE is upper

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;
}

Output:

1
2
shirley
YELRIHS

Click here to debug.

Reference 

Check if the string is uppercase or lowercase

Comments