Check if String Is Empty in C++

A String is a combination of characters that can store both alphabets and numbers together. In C, there was no concept of string as a datatype so character arrays were used. In C++, this drawback was resolved and the string was added as an individual datatype.

Ways to Check if String Is Empty in C++

This article explains four different methods to check if a string is empty or not. So, let’s dive into it.

Using Empty() method

The empty() function in C++ can be used to check if a given variable is empty or not. As it is a system function, it can also be used on datatypes other than strings.

In this example, we create a function that uses a string parameter to run checks using the empty() function and prints the result. Inside the main function, the value of the string is passed to the method.

Let’s look at a step-by-step guide to this program:

  1. Import library package iostream and define the namespace as std.
  2. A method check_emptystr is created which has a string parameter s.
  3. Inside an if-else statement, the value inside variable s is checked using the syntax if (s.empty()). If the condition is true, a relevant message is printed, while inside the else statement, the vice versa message is printed.
  4. Inside the main function, an empty string variable str is created and initialized with just double quotes.
  5. Lastly, the method is called by passing the variable str.

Output:

Using the Equality Operator

This method can be used to check if the string is empty by using the equality operator ==. An uninitialized empty string in C++ can be denoted as "". By using the equality operator, we can easily find out if the content is empty.

The program in this example has a function created that checks if the given string parameter is empty or not by using the equality operator. It returns 0 if the string is empty, otherwise, it returns 1. Inside the main function, a relevant message is printed as per the returned value.

A step-by-step guide to the program:

  1. Import the library packages string and iostream, and define the namespace as std.
  2. Create a method check_emptystr with a string variable str as the parameter.
  3. Inside the method, use the equality operator to check if the contents of the string are empty. An if-else statement is used for this purpose. If the string is found empty, the if statement returns 0, else it returns 1.
  4. Inside the main function, a new string variable is created that is passed to the method, and then some input is provided to it. Here, the function getline(cin, str1) stores the input inside the variable str.
  5. A new integer variable result is created that stores the returned result when the method is called.
  6. Using ternary operators, the program prints "empty" if the value of result is 0 else it prints "not empty".

Output:

Using length() method

The length of a string denotes its size. If the length of a string is 0 then it is said to be empty otherwise it is understood as non-empty. In this example, the program finds the length of the string that is provided as input and then checks its length.

Let’s look at the program:

  1. Import the library package iostream and define the namespace.
  2. A method check_emptystr is created having a string variable s as the parameter, the same as the last program.
  3. A new variable len is created for datatype size_t. The length of the variable is counted using the length() function and then it is stored inside this variable.
  4. An if-else statement is created, which returns 0 if the length found is zero else it returns 1.
  5. As we did in the last program, a string variable str1 is created and initialized with some input. A user-driven input can also be given by using the getline() function.
  6. Inside an integer variable, the result of the called method is stored.
  7. Using the ternary operator, a relevant message is printed as per the value returned.

Output:

Using size() method

Similar to checking the length of the string, the size() function can be used to check if a string is empty or not in a much quicker way. The program checks the size of the string and if it is found greater than zero, it denotes that the string is not empty.

Let’s check out the program:

  1. Library package iostream is imported and namespace is defined.
  2. Inside the main function, an uninitialized string variable str is created.
  3. An if statement is used to check if the size of the string is greater than 0. If it is found true, the program prints "not empty", else it prints "empty".

Output: As the string is uninitialized, the program prints the empty message.

Conclusion

This article explains four different methods to check if a string is empty or not in C++. The reader after going through the article will be able to create programs easily that check if the given string is empty or not.

That’s all about how to check if String is empty in C++.

Was this post helpful?

Related Posts

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.