How to determine if a string is empty in C language?

There are several ways in C language to determine if a string is empty.

  1. Check the length of the string using the strlen function to see if it equals 0.
if (strlen(str) == 0) {
    // 字符串为空
} else {
    // 字符串不为空
}
  1. Use the strcmp function to determine if a string is equal to an empty string.
if (strcmp(str, "") == 0) {
    // 字符串为空
} else {
    // 字符串不为空
}
  1. Check the first character of the string using array index to determine if it is ‘\0’.
if (str[0] == '\0') {
    // 字符串为空
} else {
    // 字符串不为空
}

These methods can all be used to determine if a string is empty, and you can choose the appropriate method based on the specific scenario.

bannerAds