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:
- Copy an empty string to the original string using the strcpy function.
char str[100] = "Hello";
strcpy(str, "");
- Use the memset function to set every character in the string to ‘\0’:
char str[100] = "Hello";
memset(str, 0, sizeof(str));
- 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';
}