onlinecode

Checking if a string is empty in C++

Checking if a string is empty in C++

In this post we will give you information about Checking if a string is empty in C++. Hear we will give you detail about Checking if a string is empty in C++And how to use it also give you demo for it if it is necessary.

we are going to learn about how to check if a string is empty or not in C++.

Checking string is empty

To check if a string is empty or not, we can use the built-in empty() function in C++.

The empty() function returns 1 if string is empty or it returns 0 if string is not empty.

Here is an example:

#include <iostream>#include <string.h>using namespace std;int main(){   string user = "";    if(user.empty()){        cout << "user string is empty";    }else{        cout << "user string is not empty ";    }    return 0;}

Output:

"user string is empty"

Similarly, we can also use the length() function to check if a given string is empty or not.

#include <iostream>#include <string.h>using namespace std;int main(){   string user = "";    if(user.length() === 0){        cout << "user string is empty";    }else{        cout << "user string is not empty ";    }    return 0;}

or we can use the size() function.

#include <iostream>#include <string.h>using namespace std;int main(){   string user = "";    if(user.size() === 0){        cout << "user string is empty";    }else{        cout << "user string is not empty ";    }    return 0;}

Note: The length() or size() functions returns the total number of characters in a given string.

Hope this code and post will helped you for implement Checking if a string is empty in C++. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Exit mobile version