C program to delete all occurrences of character from string

C program to delete all occurrences of character from string:
If you are using any software then below program will not give an error, but if you are using TURBO C then you have to make some changes like: void main() and some function like clrscr() or getch().

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

void del(char str[],char ch); //Function Prototype

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

   printf("Enter any string : ");
   gets(str);

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

   del(str,ch);  //Function Called
}

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("\nString after deleting character %c is: %s",ch,str1);
}

OUTPUT:

Enter any string : EasyLearning

Enter character you want to delete from string: a

String after deleting character a is: EsyLerning

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.