Delete from String
In this program we are going to search through a string for a character and delete it from the string.
To learn basics of string in C programming Click Here.
Example :
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str[100],ch;
char chr;
int i,j;
clrscr();
//Taking input string from user
printf("\nEnter a string:");
gets(str);
//Taking character from user to delete
printf("\nEnter a Character to delete from string:");
scanf("%c",&ch);
//Accessing each element of string to perform delete operation
for(i=0;i<strlen(str);i++)
{
if(ch==str[i])
{
//After deleting user provided element moving each element one step forward to fill //it's place
for(j=i;j<strlen(str);j++)
str[j]=str[j+1];
}
}
printf("\nString after deleting character:\n");
puts(str);
getch();
} |
Output :
For More GTU C Programming Lab Experiments Click Here