Table of Contents
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:
- Import library package
iostream
and define thenamespace
asstd
. - A method
check_emptystr
is created which has a string parameters
. - Inside an if-else statement, the value inside variable
s
is checked using the syntaxif (s.empty())
. If the condition is true, a relevant message is printed, while inside theelse
statement, the vice versa message is printed. - Inside the main function, an empty
string
variablestr
is created and initialized with just double quotes. - Lastly, the method is called by passing the variable
str
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <iostream> using namespace std; void check_emptystr(string s) { if (s.empty()) cout << "String is empty"; else cout << "String is not empty"; } int main() { string str=""; check_emptystr(str); } |
Output:
1 2 3 |
String is empty |
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:
- Import the library packages
string
andiostream
, and define thenamespace
asstd
. - Create a method
check_emptystr
with a string variablestr
as the parameter. - 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.
- 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 variablestr
. - A new integer variable
result
is created that stores the returned result when the method is called. - Using ternary operators, the program prints
"empty"
if the value ofresult
is 0 else it prints"not empty"
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include <string> #include <iostream> using namespace std; int check_emptystr(string str) { if (str == "") { return 0; } else { return 1; } } int main() { string str1; getline(cin, str1); int result = check_emptystr(str1); result == 0 ? cout << "EMpty" : cout << "Not empty"; } |
Output:
1 2 3 4 |
Enter an input - Sun Not empty |
Further reading:
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:
- Import the library package
iostream
and define thenamespace
. - A method
check_emptystr
is created having a string variables
as the parameter, the same as the last program. - A new variable
len
is created for datatypesize_t
. The length of the variable is counted using thelength()
function and then it is stored inside this variable. - An if-else statement is created, which returns 0 if the length found is zero else it returns 1.
- 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 thegetline()
function. - Inside an integer variable, the result of the called method is stored.
- Using the ternary operator, a relevant message is printed as per the value returned.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#include <iostream> using namespace std; int check_emptystr(string s) { size_t len = s.length(); if (len == 0) { return 0; } else { return 1; } } int main() { string str1 = "Scope is everywhere"; int result = check_emptystr(str1); result == 0 ? cout << "Empty" : cout << "Not empty"; } |
Output:
1 2 3 |
Not empty |
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:
- Library package
iostream
is imported andnamespace
is defined. - Inside the main function, an uninitialized string variable
str
is created. - 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"
.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#include <iostream> using namespace std; int main() { string str; if (str.size()>0){ cout<<"not empty"; } else cout << "Empty"; } |
Output: As the string is uninitialized, the program prints the empty message.
1 2 3 |
Empty |
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++.