In this article, we are going to learn How to Check if a String Is Empty in C++. We will learn multiple techniques to
find out if a string is empty or not. The different methods that we are going to learn in this article are:
- Using size() method to check if string is empty in C++.
- Using the strlen() method to check if string is empty in C++.
- Using the empty() method to check if string is empty in C++.
1.using size() method to check if string is empty in C++
In this first method that we are going to learn today is by using the size() function. In this example, we are going to check the size of the string to judge if this is an empty string or a non-empty string. We will make use of simple if-else statements to print the results based on a string is empty or not.
C++ Program to check if String is empty in C++
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int main() {
string str("Hello!! I am not empty!");
string str2;
if (str.size())
{
cout << "The string is not empty and it have text of length " << str.size() << endl;
}
else
cout << "The string is empty!" << endl;
if (str2.size())
{
cout << "The string is not empty and it have text of length " << str.size() << endl;
}
else
cout << "The string is empty!" << endl;
return 0;
}
Output:
The string is not empty and it have text of length 23
The string is empty!
2.Using strlen() method to check if string is empty in C++
In this example, we are going to use the same concept or logic that we used in our example above. The difference in this example is that we are going to use another function to judge the length of a string.strlen() is another function that can help us to find the string length and once we get the length we can easily judge if this string is empty or not. so let us see this with our example below.
C++ Program to check if string is empty using strlen() function
#include <iostream>
#include <string.h>
#include <cstring>
using namespace std;
int main() {
char *str = "Hello!! I am not empty!";
char *str2 = "";
if (strlen(str))
{
cout << "The string is not empty and it have text of length " << strlen(str) << endl;
}
else
cout << "The string is empty!" << endl;
if (strlen(str2))
{
cout << "The string is not empty and it have text of length " << strlen(str2) << endl;
}
else
cout << "The string is empty!" << endl;
return 0;
}
Output
The string is not empty and it have text of length 23
The string is empty!
3. Using empty() method to check if string is empty in C++
In this example, we are going to learn our third method to check if a string is empty or not. Here we are going to make use of the empty() function. The string class provides us with this function to check if a string is empty or not. Let us understand this with the help of the below code example.
C++ Program to check string is empty
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int main() {
string str("Hello!! I am not empty!");
string str2;
if (str.empty())
{
cout << "The string is not empty and it have text of length " << str.size() << endl;
}
else
cout << "The string is empty!" << endl;
if (str2.empty())
{
cout << "The string is not empty and it have text of length " << str.size() << endl;
}
else
cout << "The string is empty!" << endl;
return 0;
}
Output
The string is empty!
The string is not empty and it have text of length 23
Summary
In this post, we have learned different ways of how to Check if String Is Empty in C++