In this tutorial you will learn about the C Program to Remove First Occurrence of a Character in a String and its application with practical example.
C Program to Remove First Occurrence of a Character in a String
In this tutorial, we will learn to create a C program that will Remove the First Occurrence of a Character in a String in C programming
Before starting with this tutorial we assume that you are best aware of the following C programming topics:
- Operators in C Programming.
- Basic Input and Output function in C Programming.
- Basic C programming.
- For loop in C programming.
- Conditional Statements in C programming.
What is a string?
As we all know the String is a collection of characters and words. The word is a collection of alphabets. For string, only one variable is declared which can store multiple values. The String can consist of all the typeable data it means Digits, Alphabets, Symbols, etc.
1. Declaring the variables for the program.
2. Taking the input string from the user.
3. Finding the characters in string.
4. Passing those variables to for loop.
5. Using conditional statements for the program to removing the first occerence character.
6. Printing the result string.
7. End the program.
Removing the First Occurrence of a Character in a String
In this program first, we will take input string from the user. Then we will take the character to be removed from the user. Then will be Removing the First Occurrence of the Character in that String. Printing the result string.
Let us take the example program from the below code for Removing the First Occurrence of the Character in that String.
/* C Program to Remove First Occurrence of a Character in a String */
//declaring the required vaiable for the prorgam.
char strg, chr;
int i, len;
i = 0;
//Taking input string from the user.
printf("\n Please Enter any String : ");
//Taking the character to be removed.
printf("\n Please Enter the Character that you want to Remove : ");
len = strlen(strg);
//Removing that first occurence character.
while(i < len && strg[i] != chr)
while(i < len)
strg[i] = strg[i + 1];
//printing the string after removing the character.
printf("\n The Final String after Removing First occurrence of '%c' = %s ", chr, strg);
In the above program, we have first initialized the required variable.
- strg = it will hold the string value.
- chr = it will hold the string value.
- len = it will hold the integer value.
- i = it will hold the integer value.
Input string from the user for the program and Take the input character from the user to remove.
Removing that character from the string.
Printing output String.