How to Check if a String is Empty in C++? Last Updated : 01 Feb, 2024 Comments Improve Suggest changes Like Article Like Report In C++, strings are the sequence of characters that are stored as std::string class objects. In this article, we will learn how to check if a string is empty in C++ Example Input: str1="Hello! Geek" ; str2="" Output: str1 is not empty str2 is emptyChecking if the String is Empty in C++To check for an empty string we can use the std::string::empty() function because what the empty function does is it checks for the length of a string and if the string is empty, it returns true, otherwise, it returns false. C++ Program to Check for Empty StringThe below example demonstrates the use of the empty function to check for empty string. C++ // C++ program to check for empty string #include <iostream> #include <string> using namespace std; int main() { // declaring teo strings string str1 = "Hello! Geek"; string str2 = ""; // checking if the given string is empty or not if (str1.empty()) { // string is found empty cout << "str1 is empty" << endl; } else { cout << "str1 is not empty" << endl; } if (str2.empty()) { cout << "str2 is empty" << endl; } else { cout << "str2 is not empty" << endl; } return 0; } Outputstr1 is not empty str2 is empty We can also use compare() and size() functions to check if string is empty. Comment More infoAdvertise with us Next Article How to Check if a String is Empty in C++? O officialsi8v5f Follow Improve Article Tags : C++ Programs C++ cpp-string CPP Examples Practice Tags : CPP Similar Reads How to Check if a Set is Empty in C++? In C++, a set is an associative container that stores unique elements in a sorted order. In this article, we'll explore different approaches to check if a set is empty in C++ STL. Check if a Set is Empty or Not in C++To check if a std::set is empty in C++, we can use the std::set::empty() function. 2 min read How to Check if a List is Empty in C++? In C++, a list is a sequence container that allows non-contiguous memory allocation and is implemented using a doubly linked list. In this article, we will learn how to check if a list is empty in C++. Example: Input: myList = {1, 2, 3}; Output: List is not empty.Check if a List is Empty in C++To ch 2 min read How to Check if a Stack is Empty in C++? In C++, we have a stack data structure that follows a LIFO (Last In First Out) rule of operation. In this article, we will learn how to check if a stack is empty in C++. Example:Input:myStack = {1, 2, 3 } Output:Stack is not EmptyChecking if a Stack is Empty in C++To check if a stack is empty in C++ 2 min read How to Check if a Map is Empty in C++? In C++, a map is an associative container that stores elements as key-value pairs and an empty map means it contains no elements. In this article, we will learn how to check if a map is empty or not in C++. Example: Input: map<int,string>mp1 = {{1, "Ram"}, {2, "Mohit"}};map<int,string> m 2 min read How to Check if a Vector is Empty in C++? A vector is said to be empty when there are no elements present in vector. In this article, we will learn different ways to check whether a vector is empty or not.The most efficient way to check if the vector is empty or not is by using the vector empty() function. Letâs take a look at a simple exam 2 min read How to Check if a Deque is Empty in C++? In C++, a deque is a container provided by the STL library that is similar to a queue. However, unlike queues, it allows insertion and deletion from both ends. In this article, we will learn how to determine whether a deque is empty or not in C++. Example: Input: myDeque = {2, 4, 6 } Output: dq1 is 2 min read How to Check if an Array is Empty in C++? In C++, arrays are fixed-size data structures that allow the users to store similar data in contiguous memory locations. In this article, we will learn how to check if an array is empty in C++. Example: Input:arr[]={}arr[]={1,2,3,4,5}Output:The array is emptyThe array is not emptyCheck if an Array i 3 min read How to Check if a Substring Exists in a Char Array in C++? In C++, Char array and std::string both are used to store a sequence of characters to represent textual data. In this article, we will learn how to check if a substring exists within a char array in C++. Example Input: charArray[]= "Hello, Geek" subString="Geek" Output: Geek substring is found in ch 2 min read How to Convert a std::string to char* in C++? In C++, strings are the textual data that is represented in two ways: std::string which is an object, and char* is a pointer to a character. In this article, we will learn how to convert a std::string to char* in C++. Example Input:string myString = "GeeksforGeeks";Output:char * myString = "Geeksfor 1 min read How to Create a Stack of Strings in C++? In C++, Stacks are a type of container adaptor with a LIFO(Last In First Out) type of working, where a new element is added at one end (top) and an element is removed from that end only. In this article, we will learn how to create a stack of strings in C++. Creating a Stack of Strings in C++To crea 1 min read Like