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:

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++.

Leave a Comment

Your email address will not be published. Required fields are marked *

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

Scroll to Top