Home » C Programs » C Program to Delete a Character From a String

C Program to Delete a Character From a String

In this c program to delete a character from a string , you will learn how to write a C program to remove or delete a character from a string in c. This program type is distinct from number. Before you begin building such a program, you need be familiar with some fundamental String operations as well as how to work with characters and strings.

String Definition in C

Strings are defined as a collection of characters. The only difference between a character array and a string is that the string ends with the special character ‘0’. String declaration: A string can be declared in the same way as a one-dimensional array can.

Conio Definition in C

conio. h is a C header file that is primarily used by MS-DOS compilers to provide console input/output. It is an abbreviation for console input output header file. It is used in the g functions clrscr, getch, delline, getche, kbhit, gotoxy, wherex, wherey, textcolor, textbackground.

If Else Statement in C

If a particular condition is true, the if/else statement executes a block of code. If the condition is not met, another piece of code can be run. The if-else statement executes both the true and false parts of a given condition. The if block code is run if the condition is true, and the else block code is executed if the condition is false.

C Program to Delete a Character From a String

//C Program to Delete a Character From a String
#include<stdio.h>
#include<conio.h>
#include<string.h>
void removeChar(char * str, char charToRemmove){
    int i, j;
    int len = strlen(str);
    for(i=0; i<len; i++)
    {
        if(str[i] == charToRemmove)
        {
            for(j=i; j<len; j++)
            {
                str[j] = str[j+1];
            }
            len--;
            i--;
        }
    }
    
}

void main(){
	 char str[100],ch,c;
	 int i,j,len;
	 printf("Enter the String\n");
	 scanf("%[^\n]%c",str,&c);
	 printf("Enter the Character to Remove ");
	 scanf("%c",&ch);
	 removeChar(str,ch);
	 printf("After Removed '%c': %s", ch, str);
	 getch();
 } 

Output :

Enter the String
inlarn
Enter the Character to Remove n
After Removed ‘n’: ilar