Check if String Is Empty in C++

  1. Use Built-In Method empty() to Check if String Is Empty in C++
  2. Use Custom Defined Function With size to Check if String Is Empty in C++
  3. Use the strlen() Function to Check if String Is Empty in C++

This article will introduce multiple methods about how to check for an empty string in C++.

Use Built-In Method empty() to Check if String Is Empty in C++

The std::string class has a built-in method empty() to check if the given string is empty or not. This method is of type bool and returns true when the object does not contain characters. The empty() method does not take any argument and implements a constant time complexity function.

Note that even if the string is initialized with an empty string literal - "", the function still returns the true value. Thus, the empty() function checks for the size of the string and essentially returns the evaluation of the expression - string.size() == 0.

#include <iostream>
#include <string>
#include <cstring>

using std::cout; using std::endl;
using std::cin; using std::string;

int main() {
    string string1("This is a non-empty string");
    string string2;

    string1.empty() ?
        cout << "[ERROR] string is empty!" << endl :
        cout << "string value: " << string1 << endl;

    string2.empty() ?
        cout << "[ERROR] string is empty!" << endl :
        cout << "string value: " << string1 << endl;

    return EXIT_SUCCESS;
}

Output:

string value: This is a non-empty string
[ERROR] string is empty!

Use Custom Defined Function With size to Check if String Is Empty in C++

The previous method could be implemented by the user-defined function that takes a single string argument and checks if it is empty. This function would mirror the behavior of the empty method and return a bool value. The following example demonstrates the same code example with the custom-defined function checkEmptyString. Notice that the function contains only one statement as the return type of the comparison expression would be boolean, and we can directly pass that to the return keyword.

#include <iostream>
#include <string>
#include <cstring>

using std::cout; using std::endl;
using std::cin; using std::string;

bool checkEmptyString(const string &s)
{
    return s.size() == 0;
}

int main() {
    string string1("This is a non-empty string");
    string string2;

    checkEmptyString(string1) ?
        cout << "[ERROR] string is empty!" << endl :
        cout << "string value: " << string1 << endl;

    checkEmptyString(string2) ?
        cout << "[ERROR] string is empty!" << endl :
        cout << "string value: " << string1 << endl;

    return EXIT_SUCCESS;
}

Output:

string value: This is a non-empty string
[ERROR] string is empty!

Use the strlen() Function to Check if String Is Empty in C++

The strlen() function is part of the C string library and can be utilized to retrieve the string’s size in bytes. This method could be more flexible for both string and char* type strings that may come up in the codebase. strlen takes const char* argument and calculates the length excluding the terminating \0 character.

Note that the program’s structure is similar to the previous methods, as we define a separate boolean function and declare the only parameter it takes as const char*.

#include <iostream>
#include <string>
#include <cstring>

using std::cout; using std::endl;
using std::cin; using std::string;

bool checkEmptyString(const char *s)
{
    return strlen(s) == 0;
}

int main() {
    string string1("This is a non-empty string");
    string string2;

    checkEmptyString(string1.data()) ?
        cout << "[ERROR] string is empty!" << endl :
        cout << "string value: " << string1 << endl;

    checkEmptyString(string2.data()) ?
        cout << "[ERROR] string is empty!" << endl :
        cout << "string value: " << string1 << endl;

    return EXIT_SUCCESS;
}

Output:

string value: This is a non-empty string
[ERROR] string is empty!
Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - C++ String

  • Convert String to Lower Case in C++
  • Parse String Using a Delimiter in C++