C program to remove the last occurrence of a character from a string – In this article, we will brief in on the various ways to remove the last occurrence of a character from a string in C programming.
Suitable examples and sample programs have also been added so that you can understand the whole thing very clearly. The compiler has also been added with which you can execute it yourself.
The ways used in this piece are as follows:
- Using Standard Method
- Using Function
- Using Recursion
A string is nothing but an array of characters. The value of a string is determined by the terminating character. Its value is considered to be 0.
As we can see in the image uploaded above, we have to enter a string first.
The string entered here is as follows:
“welcome to c beginners”
Then we have to enter the character whose last occurrence we wanna delete.
The character is specified to be ‘c’.
Hence, the final string becomes
“welcome to beginners”
Thus, means to do the same in C programming are as follows:
Using Standard Method
- Read user entered string using gets(s) function.
2) Read the entered character using getchar() and store in the variable c.
3) The first for loop iterates through the given string until the last character becomes to null with the structure for(i=0;s[i];i++).
4) Initialize n=i.i value represents the length of the string.
5) While loop iterates the code if ‘i’ value not equal to 0.
a) If temp=1 then decrease the i value and the inner if compares s[i] with the removable character. If it matches then shift the element which is right to the removed element, to one position left and assign temp=0.
b) If temp=0.
if i=n then break the loop.
if i!=n then shift the remaining elements which are right to the removed element to one position left as s[i]=s[i+1] and increase i value.
6) Print the string after removing the last occurrence of the element.
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 45 46 47 48 49 |
#include <stdio.h> #include <string.h> int main() { char s[1000],c,temp=1; int i,j,k,count=0,n; printf("Enter the string : "); gets(s); printf("Enter character: "); c=getchar(); for(i=0;s[i];i++); n=i; while(i) { if(temp) { i--; if(c==s[i]) { temp=0; s[i]=s[i+1]; i++; } } else { if(i==n) break; s[i]=s[i+1]; i++; } } printf("%s",s); return 0; } |
Output:
1 2 3 |
Enter the string: welcome to c beginners Enter character: c welcome to beginners |
Using Function
- The main() calls the stringlength(char *s) to get the length of the string.
2) The main() calls the deletechar(char *s, char c) to remove the last occurrence of the element in the string.
Initialize temp=1 and i,n with the length of the string.
while loop iterates until i becomes to 0.
a) Checks the temp value if temp=1 then inner if compares the s[i] with the entered character. If it matches then initialize temp=0 and shift the element which is right to the removable element to one position left.
b) if temp=0
If ‘i’ equal to n then break the loop. Otherwise, shift the remaining elements which are right to the removed element, to one position left by increasing the i value.
3) The main() prints the string which has the less string length than the original string.
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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
#include <stdio.h> #include <string.h> int stringlength(char *s) { int j; for(j=0;s[j];j++); return j; } void deletechar(char *s,char c) { int i,temp=1,n; i=n=stringlength(s); while(i) { if(temp) { i--; if(c==s[i]) { temp=0; s[i]=s[i+1]; i++; } } else { if(i==n) break; s[i]=s[i+1]; i++; } } } int main() { char s[1000],c; printf("Enter the string : "); gets(s); printf("Enter character: "); c=getchar(); deletechar(s,c); printf("string after removing last occurance of character %c:\n ",c); printf("%s",s); return 0; } |
Output:
1 2 3 4 |
Enter the string: hello world Enter character: o string after removing last occurrence of character o: hello wrld |
Using Recursion
- The stringlength(char *s) returns the length of the string.
2) The main() function calls the recursive function deletechar(char *s,char c).
Initialize i=length of the string.
A) If the length of the string!=0
if temp=1
a) then decrease the i value.
b) If the element s[i] match with the entered character then initialize temp=0 and shift the right element to the removable element, to one position left and increase the i value.
B) If the length of the string is zero.
If i=n then break the loop. Otherwise, shift the remaining elements which are right to the removed element to one position left by increasing the i value.
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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
#include <stdio.h> #include <string.h> int stringlength(char *s) { int j; for(j=0;s[j];j++); return j; } void deletechar(char *s,char c) { static int i=stringlength(s),temp=1,n=i; if(i) { if(temp) { i--; if(c==s[i]) { temp=0; s[i]=s[i+1]; i++; } } else { if(i==n) return; s[i]=s[i+1]; i++; } deletechar(s,c); } } int main() { char s[1000],c; printf("Enter the string : "); gets(s); printf("Enter character: "); c=getchar(); deletechar(s,c); printf("string after removing last occurance of character %c:\n ",c); printf("%s",s); return 0; } |
1 2 3 4 |
Enter the string: welcome to c beginners Enter character: b string after removing last occurrence of character b: welcome to c eginners |