Programming for Problem Solving

GTU Practical 39

39. Write a program to delete a character in the given string.
#include<stdio.h>  ( this applies in the given code )

#include

int main() {
char str[100], ch;

printf(“Enter a string: “);
fgets(str, sizeof(str), stdin);

printf(“Enter the character to delete: “);
scanf(“%c”, &ch); // Fixes issue by reading the next character input

int i, j = 0;
while (str[i] != ‘\0’) {
if (str[i] != ch) {
str[j++] = str[i];
}
i++;
}
str[j] = ‘\0’;

printf(“Modified string: %s\n”, str);

return 0;
}

OUTPUT

Enter a string: df
Enter the character to delete: f
Segmentation fault

GTU STUDY