How to check if a string is empty in C

How to check if a string is empty in C

In this post we will give you information about How to check if a string is empty in C. Hear we will give you detail about How to check if a string is empty in CAnd 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 C string is empty or not using the strlen() function.

Checking the empty string

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

The strlen() function takes the string as an argument and returns the total number of characters in a given string, so if that function returns 0 then the given string is empty else it is not empty.

Here is an example:

#include <stdio.h>#include <string.h>int main(){    char str[] = "";    if(strlen(str) == 0){        printf("string is empty");    }else{         printf("string is not  empty");    }    return 0;}

Second way

In C language strings are terminated with a null character , so we can check if a given string is empty or not by verifying if a string contains the first character is .

#include <stdio.h>#include <string.h>int main(){    char str[] = "";    if(str[0] == ''){        printf("string is empty");    }else{         printf("string is not  empty");    }    return 0;}

Note: C language doesn’t have any built-in function to check if a string is empty, like we have a empty() function in C++.

Hope this code and post will helped you for implement How to check 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

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US