How to check if a word is all uppercase or all lowercase or capitalized in C++

2 Answers

0 votes
#include <iostream>
#include <string>
#include <cctype> 
 
bool isAllUpper(const std::string& word) {
    for (char ch : word) {
        if (!std::isupper(static_cast<unsigned char>(ch))) return false;
    }

    return true;
}
 
bool isAllLower(const std::string& word) {
    for (char ch : word) {
        if (!std::islower(static_cast<unsigned char>(ch))) return false;
    }

    return true;
}
 
bool isCapitalized(const std::string& word) {
    if (word.empty()) return false;
    if (!std::isupper(static_cast<unsigned char>(word[0]))) return false;
    for (size_t i = 1; i < word.size(); i++) {
        if (!std::islower(static_cast<unsigned char>(word[i]))) return false;
    }

    return true;
}
 
void runTest(const std::string& word) {
    std::cout << "Testing word: \"" << word << "\"\n";

    if (isAllUpper(word)) {
        std::cout << "OK - All uppercase\n";
    } else if (isAllLower(word)) {
        std::cout << "OK - All lowercase\n";
    } else if (isCapitalized(word)) {
        std::cout << "OK - Capitalized\n";
    } else {
        std::cout << "Error - Mixed casing\n";
    }
}
 
int main() {
    runTest("PROGRAMMING"); 
    runTest("programming"); 
    runTest("Programming"); 
    runTest("ProGramMing"); 
}
 
 
 
/*
run:
 
Testing word: "PROGRAMMING"
OK - All uppercase

Testing word: "programming"
OK - All lowercase

Testing word: "Programming"
OK - Capitalized

Testing word: "ProGramMing"
Error - Mixed casing
 
*/

 



answered Oct 26, 2025 by avibootz
edited Oct 27, 2025 by avibootz
0 votes
#include <iostream>
#include <string>

// Checks if the word is all uppercase, all lowercase, or capitalized
bool verifyAllUpperORAllLowerORIsCapitalized(std::string word) {
    int upper = 0;
    int lower = 0;
    
    for (auto ch : word) {
        if (ch >= 'a' && ch <= 'z') {
            lower++;
        } else { 
                if (ch >='A' && ch <= 'Z') {
                    upper++;
                }
        }
    }
    if (upper == 0) return true; // all lowercase
    if (lower == 0) return true; // all uppercase
    if (upper == 1 && (word[0] >= 'A' && word[0] <= 'Z')) return true;  // capitalized
    
    return false;
}

// Runs a test case and prints the result
void runTest(const std::string& word) {
    std::cout << "Testing word: \"" << word << "\"\n";

    if (verifyAllUpperORAllLowerORIsCapitalized(word)) {
        std::cout << "OK\n";
    } else if (verifyAllUpperORAllLowerORIsCapitalized(word)) {
        std::cout << "OK\n";
    } else if (verifyAllUpperORAllLowerORIsCapitalized(word)) {
        std::cout << "OK\n";
    } else {
        std::cout << "Error\n";
    }
}

int main() {
    runTest("PROGRAMMING");  
    runTest("programming");  
    runTest("Programming");  
    runTest("ProGramMing");  
}



/*
run:

Testing word: "PROGRAMMING"
OK

Testing word: "programming"
OK

Testing word: "Programming"
OK

Testing word: "ProGramMing"
Error

*/

 



answered Oct 26, 2025 by avibootz
edited Oct 27, 2025 by avibootz
...