Problem 39

author
By Supreme2023-03-15
Problem Statement :
Write a program to delete a character in given string.
Code:

#include <stdio.h>

void removeChar(char* str, char ch) {
int i, j;
int len = strlen(str);
for (i = j = 0; i < len; i++) {
if (str[i] != ch) {
str[j++] = str[i];
}
}
str[j] = '\0';
}

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

printf("\tDelete Character");
printf("\n====================================\n\n");

printf("Enter The String : ");
gets(str);
printf("\nEnter The Character To Replace : ");
scanf(" %c",&ch);

removeChar(str, ch);

printf("\n-------------------------------------\n");
printf("\nString After Removing %c : %s\n",ch, str);
printf("\n-------------------------------------\n");
return 0;
}

Description :
  • arguments: a pointer to a character string and a character to remove from the string.
  • The function removes all occurrences of the character ch from the input string by iterating through the string and copying each character that is not equal to ch to a new string.
  • The new string is then null-terminated and returned.
  • In the main function, the user is prompted to enter a string and a character to remove.
  • The removeChar function is called with the string and character arguments, and the resulting string with the specified character removed is printed to the console.
Output Image:
Output-image

© Copyright 2022. All right reserved, GTU Coder.