Check if String is Empty in C++

In this article, we will discuss different ways to check if a string is empty or not in C++.

Table of Contents

Check if String is Empty in C++ using string::empty()

In C++, the string class provides a member function empty(), which returns True if the length of the calling string object is 0, otherwise returns False. Let’s use this to check if String is Empty or not in following examples,

Example 1:

#include <iostream>

int main()
{
    std::string sample_str = "";

    // Check if string is empty
    if ( sample_str.empty() ) {
        std::cout<<"String is Empty" <<std::endl;
    }
    else {
        std::cout<<"String is not Empty" <<std::endl;
    }

    return 0;
}

Output:

Advertisements
String is Empty

Example 2:

#include <iostream>

int main()
{
    std::string sample_str = "Sample String";

    // Check if string is empty
    if ( sample_str.empty() ) {
        std::cout<<"String is Empty" <<std::endl;
    }
    else {
        std::cout<<"String is not Empty" <<std::endl;
    }

    

Output:

String is not Empty

If the string contains any character then the empty() function will return False.

Check if String is Empty in C++ using string::size()

In C++, the string class provides a member function size(), which returns the length of the string in bytes. If string is empty, then the size() function will return 0. Let’s use this to check if String is Empty or not in following examples,

#include <iostream>

int main()
{
    std::string sample_str = "";

    // Check if string is empty
    if ( sample_str.size() == 0 ) 
    {
        std::cout<<"String is Empty" <<std::endl;
    }
    else 
    {
        std::cout<<"String is not Empty" <<std::endl;
    }

    return 0;
}

Output:

String is Empty

Example 2:

#include <iostream>

int main()
{
    std::string sample_str = "Sample String";

    // Check if string is empty
    if ( sample_str.size() == 0 ) 
    {
        std::cout<<"String is Empty" <<std::endl;
    }
    else 
    {
        std::cout<<"String is not Empty" <<std::endl;
    }

    return 0;
}

Output:

String is not Empty

If the string contains any character then the value returned by size() function will be more than 0.

Check if String is Empty in C++ using string::length()

In C++, the string class provides a member function length(), which returns the length of the string in bytes. If string is empty, then the length () function will return 0. Let’s use this to check if String is Empty or not in following examples,

Example 1:

#include <iostream>

int main()
{
    std::string sample_str = "";

    // Check if string is empty
    if ( sample_str.length() == 0 ) 
    {
        std::cout<<"String is Empty" <<std::endl;
    }
    else 
    {
        std::cout<<"String is not Empty" <<std::endl;
    }

    return 0;
}

Output:

String is Empty

Example 2:

#include <iostream>

int main()
{
    std::string sample_str = "Some sample text";

    // Check if string is empty
    if ( sample_str.length() == 0 ) 
    {
        std::cout<<"String is Empty" <<std::endl;
    }
    else 
    {
        std::cout<<"String is not Empty" <<std::endl;
    }

    return 0;
}

Output:

String is not Empty

If the string contains any character then the value returned by length() function will be more than 0.

Check if String is Empty in C++ using strlen()

Using the c_str() member function of string, get the char pointer and pass that to the strlen() function. If it returns 0, then it means string is empty. Let’s understand this with some examples,

Example 1:

#include <iostream>
#include <cstring>

int main()
{
    std::string sample_str = "";

    // Check if string is empty
    if (strlen(sample_str.c_str()) == 0 )
    {
        std::cout<<"String is Empty" <<std::endl;
    }
    else 
    {
        std::cout<<"String is not Empty" <<std::endl;
    }

    return 0;
}

Output

String is Empty

Example 2:

#include <iostream>
#include <cstring>

int main()
{
    std::string sample_str = "This is a sample";

    // Check if string is empty
    if (strlen(sample_str.c_str()) == 0 )
    {
        std::cout<<"String is Empty" <<std::endl;
    }
    else 
    {
        std::cout<<"String is not Empty" <<std::endl;
    }

    return 0;
}

Output

String is not Empty

Summary:

We learned about four different ways to check if a string is empty in C++.

Do you want to Learn Modern C++ from best?

We have curated a list of Best C++ Courses, that will teach you the cutting edge Modern C++ from the absolute beginning to advanced level. It will also introduce to you the word of Smart Pointers, Move semantics, Rvalue, Lambda function, auto, Variadic template, range based for loops, Multi-threading and many other latest features of C++ i.e. from C++11 to C++20.

Check Detailed Reviews of Best Modern C++ Courses

Remember, C++ requires a lot of patience, persistence, and practice. So, start learning today.

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top