C program to remove last occurrence of a character from string
Learn how to write a C program to remove the last occurrence of a specified character from a string. This guide includes step-by-step instructions, code implementation, and detailed explanations for beginners in C programming.
In this article, we will explore how to write a C program that removes the last occurrence of a specified character from a given string. This task is often needed in various text processing applications, such as editing, parsing, and data cleaning.
Objective
The goal is to create a C program that takes a string and a character as inputs, finds the last occurrence of the specified character in the string, removes it, and then prints the modified string.
Approach
To solve this problem, we will:
- Read the input string from the user.
- Read the character to be removed from the user.
- Find the last occurrence of the specified character in the string.
- Remove the character by shifting subsequent characters to the left.
- Print the modified string.
Detailed Steps
- Read the input string:
- Use standard input functions to get the string from the user.
- Read the character to be removed:
- Use standard input functions to get the character from the user.
- Find the last occurrence of the character:
- Iterate over the string from the end to find the index of the last occurrence of the specified character.
- Remove the character:
- Shift the characters following the found index to the left to overwrite the character.
- Print the modified string:
- Print the resulting string after the character has been removed.
Write a C program to remove last occurrence of a character from string
Below is the complete C program that implements the above steps:
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
char char_to_remove;
int index = -1;
// Input the string
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
// Remove newline character from string if present
size_t len = strlen(str);
if (len > 0 && str[len - 1] == '\n') {
str[len - 1] = '\0';
}
// Input the character to remove
printf("Enter the character to remove: ");
scanf("%c", &char_to_remove);
// Find the last occurrence of the character
for (int i = strlen(str) - 1; i >= 0; i--) {
if (str[i] == char_to_remove) {
index = i;
break;
}
}
// If the character is found, remove it by shifting the subsequent characters
if (index != -1) {
for (int i = index; i < strlen(str); i++) {
str[i] = str[i + 1];
}
}
// Print the modified string
if (index != -1) {
printf("String after removing last occurrence of '%c': %s", char_to_remove, str);
} else {
printf("Character '%c' not found in the string.", char_to_remove);
}
return 0;
}
Output
Enter a string: learn programming at ProCoding
Enter the character to remove: a
String after removing last occurrence of 'a': learn programming t ProCoding
Explanation of the Program
- Input Handling:
- The program uses
fgets()
to read a line of text from the user, ensuring that spaces and special characters are included. - It removes any trailing newline character added by
fgets()
.
- The program uses
- Reading the Character to Remove:
- The program uses
scanf()
to read a single character input from the user.
- The program uses
- Finding the Last Occurrence:
- A
for
loop iterates over the string from the end to the beginning to find the last index where the specified character occurs. If found, the index is stored in the variableindex
.
- A
- Removing the Character:
- If the character is found (
index != -1
), another loop shifts all characters after the found index to the left by one position, effectively removing the character.
- If the character is found (
- Output:
- The modified string is printed if the character was found and removed. If the character was not found, a message is printed to inform the user.