What is the method to clear a string in the C language?

In the C language, you can use the following method to clear a string:

  1. Copy an empty string to the original string using the strcpy function.
char str[100] = "Hello";
strcpy(str, "");
  1. Use the memset function to set every character in the string to ‘\0’:
char str[100] = "Hello";
memset(str, 0, sizeof(str));
  1. Use a loop to set each character in the string to ‘\0’.
char str[100] = "Hello";
for (int i = 0; i < strlen(str); i++) {
    str[i] = '\0';
}
bannerAds