Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,849 questions

55,678 answers

573 users

How to check if a string is blank (empty, null, or contains only whitespace) in C++

1 Answer

0 votes
#include <algorithm>
#include <iostream>
#include <string>
#include <cctype>

bool isBlank(const std::string* str) {
    // Check for null pointer
    if (str == nullptr) {
        return true;
    }

    // Check if the string is empty or contains only whitespace
    return str->empty() || std::all_of(str->begin(), str->end(), [](unsigned char ch) { 
        return std::isspace(ch); });
}

int main() {
    std::string test1 = "";     // Empty string
    std::string test2 = "   ";  // Whitespace only
    std::string* test3 = nullptr; // NULL string pointer
    std::string test4 = "C++";  // Non-empty string

    std::cout << std::boolalpha; // Print boolean values as true/false
    std::cout << "Test1 is blank: " << isBlank(&test1) << std::endl;
    std::cout << "Test2 is blank: " << isBlank(&test2) << std::endl;
    std::cout << "Test3 is blank: " << isBlank(test3) << std::endl;
    std::cout << "Test4 is blank: " << isBlank(&test4) << std::endl;
}



/*
run:

Test1 is blank: true
Test2 is blank: true
Test3 is blank: true
Test4 is blank: false

*/

 



answered Jun 7, 2025 by avibootz
...