Write a program that accepts string from the user and allow the user to delete a specified character from a string inputted.
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#define BUFFER_SIZE 256
int main() {
char buffer[BUFFER_SIZE];
printf("Enter string: ");
if (!fgets(buffer, BUFFER_SIZE, stdin)) {
printf("[Error] Can't read from stdout\n");
exit(1);
}
printf("Enter a character you wish to delete: ");
char c;
scanf("%c", &c);
size_t i = 0;
size_t len = strlen(buffer);
while (i < len && buffer[i] != c) i++;
if (i != len) {
for (size_t j = i; j < len; j++) {
if (buffer[j] != c) {
buffer[i] = buffer[j];
i++;
}
}
}
buffer[i] = '\0';
printf("%s", buffer);
return 0;
}
Comments
Leave a comment