C program to remove last occurrence of a character in a string; Through this tutorial, we will learn how to remove last occurrence of a character in a string using for loop, while loop and functions in c programs.
Programs and Algorithm to Remove Last Occurrence of a Character in a String in C
- Algorithm to remove last occurrence of a character
- C Program to Remove Last Occurrence of a Character in a String using For Loop
- C Program to Remove Last Occurrence of a Character in a String using While Loop
- C Program to Remove Last Occurrence of a Character in a String using Function
Algorithm to remove last occurrence of a character
Use the following algorithm to write a c program to remove last occurrence of a character in given string; as follows:
- Start program.
- Take input string from user, store it in some variable.
- Take Input character to remove from user, store it in some variable.
- Find and remove last occurrence of the given character in string.
- Print result
- End program
C Program to Remove Last Occurrence of a Character in a String using For Loop
/* C Program to Remove Last Occurrence of a Character in a String */ #include <stdio.h> #include <string.h> int main() { char str[100], ch; int i, index, len; index = -1; printf("\n Please Enter any String : "); gets(str); printf("\n Please Enter the Character that you want to Search for : "); scanf("%c", &ch); len = strlen(str); 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++; } } printf("\n The Final String after Removing Last Occurrence of '%c' = %s ", ch, str); return 0; }
The output of the above c program; as follows:
Please Enter any String : hello world Please Enter the Character that you want to Search for : o The Final String after Removing Last Occurrence of 'o' = hello wrld
C Program to Remove Last Occurrence of a Character in a String using While Loop
/* C Program to Remove Last Occurrence of a Character in a String */ #include <stdio.h> #include <string.h> int main() { char str[100], ch; int i, index, len; index = -1; i = 0; printf("\n Please Enter any String : "); gets(str); printf("\n Please Enter the Character that you want to Search for : "); scanf("%c", &ch); len = strlen(str); while(i < len) { if(str[i] == ch) { index = i; } i++; } if(index != -1) { i = index; while(i < len) { str[i] = str[i + 1]; i++; } } printf("\n The Final String after Removing Last Occurrence of '%c' = %s ", ch, str); return 0; }
The output of the above c program; as follows:
Please Enter any String : programming world Please Enter the Character that you want to Search for : o The Final String after Removing Last Occurrence of 'o' = programming wrld
C Program to Remove Last Occurrence of a Character in a String using Function
/* C Program to Remove Last Occurrence of a Character in a String */ #include <stdio.h> #include <string.h> void Remove_LastOccurrence(char *str, char ch); int main() { char str[100], ch; printf("\n Please Enter any String : "); gets(str); printf("\n Please Enter the Character that you want to Search for : "); scanf("%c", &ch); Remove_LastOccurrence(str, ch); printf("\n The Final String after Removing Last Occurrence of '%c' = %s ", ch, str); return 0; } void Remove_LastOccurrence(char *str, char ch) { int i, index, len; len = strlen(str); 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++; } } }
The output of the above c program; as follows:
Please Enter any String : remove last character Please Enter the Character that you want to Search for : r The Final String after Removing Last Occurrence of 'r' = remove last characte