To Delete all occurrences of Character from the String


Levels of difficulty: / perform operation:

Method Used

  1. we have accepted one string from the user and character to be deleted from the user.
  2. Now we have passed these two parameters to function which will delete all occurrences of given character from the string.
  3. Whole String will be displayed as output except given Character.

C Program:

#include<stdio.h>
#include<conio.h>
#include<string.h>

void del(char str[], char ch);

void main() {
   char str[10];
   char ch;

   printf("\nEnter the string : ");
   gets(str);

   printf("\nEnter character which you want to delete : ");
   scanf("%ch", &ch);

   del(str, ch);
   getch();
}

void del(char str[], char ch) {
   int i, j = 0;
   int size;
   char ch1;
   char str1[10];

   size = strlen(str);

   for (i = 0; i < size; i++) {
      if (str[i] != ch) {
         ch1 = str[i];
         str1[j] = ch1;
         j++;
      }
   }
   str1[j] = '\0';

   printf("\ncorrected string is : %s", str1);
}

Output:

Enter the string : abhiman
Enter character which you want to delete : a
Corrected string is : bhimn