What is the usage of the ‘isEmpty’ function in C language?

There is no built-in isempty function in the C language. However, you can achieve similar functionality by creating a custom function.

Here is an example of a custom function isEmpty, which is used to determine if a string is empty:

#include <stdio.h>
#include <stdbool.h>
#include <string.h>

bool isEmpty(const char* str) {
    if (str == NULL) {
        return true;
    }
    if (strlen(str) == 0) {
        return true;
    }
    return false;
}

int main() {
    char str1[] = "";  // 空字符串
    char str2[] = "Hello";  // 非空字符串

    if (isEmpty(str1)) {
        printf("str1 is empty\n");
    } else {
        printf("str1 is not empty\n");
    }

    if (isEmpty(str2)) {
        printf("str2 is empty\n");
    } else {
        printf("str2 is not empty\n");
    }

    return 0;
}

When running the above code, the output result is:

str1 is empty
str2 is not empty

It is important to note that the custom isEmpty function mentioned above can only determine if a string is empty and not other types of variables.

Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds