In this tutorial you will learn about the C Program to Remove Last Occurrence of a Character in a String and its application with practical example.
C Program to Remove Last Occurrence of a Character in a String
In this tutorial, we will learn to create a C program that will Remove the Last Occurrence of a Character in a String in C programming
Prerequisites
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.
Algorithm:-
1 2 3 4 5 6 7 8 9 10 11 12 13 |
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 Last occerence character. 6. Printing the result string. 7. End the program. |
Removing the Last Occurrence of a Character in a String
In this program first, we will take input strings from the user. Then we will take the character to be removed from that string. Then will be Removing the Last Occurrence of the Character in that String. Printing the result string.
Let us take the example program from the below code for Removing the Last Occurrence of the Character in that String.
Program Code:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
/* ** C Program to Remove Last Occurrence of a Character in a String ** */ #include <stdio.h> #include <string.h> int main() { //declaring the required variable for the program. char str[100], ch; int i, index, len; index = -1; //Taking the input string from the user printf("\n Please Enter any String : "); gets(str); //Taking the character to be removed from the string printf("\n Please Enter the Character that you want to Search for : "); scanf("%c", &ch); len = strlen(str); //Removing the lasat occurence of that character for(i = 0; i < len; i++) { if(str[i] == ch) { index = i; } } if(index != -1) { i = index; while(i < len) { str[i] = str[i + 1]; i++; } } //Printing the result string after removing. printf("\n The Final String after Removing Last Occurrence of '%c' = %s ", ch, str); return 0; } |
Output:-
In the above program, we have Last initialized the required variable.
- str[100] = it will hold the string value.
- ch = 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.