c++ check if string contains uppercase

To check if a string contains an uppercase letter in C++, you can iterate through each character of the string and use the isupper() function from the <cctype> library to check if each character is uppercase. Here's the code:

#include <iostream>
#include <cctype>

bool containsUppercase(const std::string& str) {
    for (char c : str) {
        if (std::isupper(c)) {
            return true; // If an uppercase character is found, return true
        }
    }
    return false; // If no uppercase character is found, return false
}

int main() {
    std::string str = "Hello World";
    if (containsUppercase(str)) {
        std::cout << "The string contains an uppercase letter." << std::endl;
    } else {
        std::cout << "The string does not contain an uppercase letter." << std::endl;
    }
    return 0;
}

In this code, the containsUppercase() function takes a string str as input and returns a boolean value indicating whether the string contains an uppercase letter or not.

The function iterates through each character of the string using a range-based for loop, and for each character c, it checks if std::isupper(c) returns true. The std::isupper() function is a part of the <cctype> library and it checks if a character is uppercase.

If an uppercase character is found, the function immediately returns true. If no uppercase character is found after iterating through all the characters, the function returns false.

In the main() function, we create a string str and pass it to the containsUppercase() function. Depending on the return value, we print either "The string contains an uppercase letter." or "The string does not contain an uppercase letter."