Remove Character from C String
To remove a specified character, you can use two pointers to traverse the string and keep the characters that should not be deleted. Below is an example program:
#include <stdio.h>
#include <string.h>
void removeChar(char *str, char c) {
int i, j = 0;
for (i = 0; str[i] != '\0'; i++) {
if (str[i] != c) {
str[j++] = str[i];
}
}
str[j] = '\0';
}
int main() {
char str[] = "Hello, World!";
char c = 'l';
printf("Original string: %s\n", str);
removeChar(str, c);
printf("String after removing '%c': %s\n", c, str);
return 0;
}
In the example above, the removeChar function takes a string and a character as parameters, then iterates through the string to keep only the characters that are not equal to the specified character. Finally, it prints out the modified string.