C program to remove last occurrence of a word from string
Learn how to write a C program to remove the last occurrence of a word from a string. This tutorial includes step-by-step instructions, code implementation without and with the strstr function, and explanations to help you understand the process of removing a word from a string using basic string manipulation techniques.
String manipulation is a fundamental aspect of programming. One common task is removing the last occurrence of a specific word from a string. This article will guide you through writing a C program to accomplish this task using basic string manipulation techniques. We will first implement the logic manually without using strstr, and then use the strstr function to achieve the same result.
Steps to Solve the Problem
- Input the String and Word: Read the input string and the word to be removed.
- Search for the Last Occurrence of the Word: Find the last occurrence of the word in the string.
- Remove the Word: Modify the string to remove the word.
- Print the Result: Output the modified string.
Write a C program to remove last occurrence of a word from string
Here is the C program to remove the last occurrence of a word from a given string:
#include <stdio.h>
#include <string.h>
int main() {
char str[200], word[20];
int i, j, k, pos = -1;
int strLen, wordLen;
// Input the string
printf("Enter the string: ");
fgets(str, sizeof(str), stdin);
str[strcspn(str, "\n")] = '\0'; // Removing trailing newline character
// Input the word to be removed
printf("Enter the word to remove: ");
scanf("%s", word);
strLen = strlen(str);
wordLen = strlen(word);
// Search for the last occurrence of the word in the string
for (i = 0; i <= strLen - wordLen; i++) {
for (j = 0; j < wordLen; j++) {
if (str[i + j] != word[j]) {
break;
}
}
if (j == wordLen) {
pos = i; // Update position to the last found occurrence
}
}
// If word is found
if (pos != -1) {
// Shift the remaining characters to the left
for (k = pos; k <= strLen - wordLen; k++) {
str[k] = str[k + wordLen];
}
str[strLen - wordLen] = '\0'; // Terminate the string
printf("Modified string: %s", str);
} else {
printf("Word not found in the string.");
}
return 0;
}
Output
Enter the string: The fog grew thicker and thicker, obscuring the path ahead
Enter the word to remove: thicker
Modified string: The fog grew thicker and , obscuring the path ahead
Explanation of the Code
- Input Handling: We read the input string using
fgetsand remove the trailing newline character. We then read the word to be removed usingscanf. - Search for the Last Occurrence: We use nested loops to search for the last occurrence of the word in the string.
- The outer loop iterates over each position in the string where the word could start.
- The inner loop checks if the word matches the substring starting at the current position.
- We keep updating the position variable to the last found occurrence of the word.
- Remove the Word:
- If the word is found, we shift the characters to the left starting from the position after the word to the end of the string.
- This effectively removes the word from the string.
- Print the Result: We print the modified string. If the word is not found, we print a message indicating that the word was not found.
Implementation Using strstr
Here is the C program to remove the last occurrence of a word from a given string using the strstr function:
#include <stdio.h>
#include <string.h>
int main() {
char str[200], word[20];
char *pos, *lastPos = NULL;
// Input the string
printf("Enter the string: ");
fgets(str, sizeof(str), stdin);
str[strcspn(str, "\n")] = '\0'; // Removing trailing newline character
// Input the word to be removed
printf("Enter the word to remove: ");
scanf("%s", word);
// Find the position of the last occurrence of the word
pos = strstr(str, word);
while (pos != NULL) {
lastPos = pos;
pos = strstr(pos + 1, word);
}
if (lastPos != NULL) {
// Calculate lengths
int len = strlen(word);
int i;
// Remove the word by shifting the remaining characters to the left
for (i = 0; lastPos[i + len] != '\0'; i++) {
lastPos[i] = lastPos[i + len];
}
lastPos[i] = '\0'; // Terminate the string
printf("Modified string: %s", str);
} else {
printf("Word not found in the string.");
}
return 0;
}
Explanation of the Code
- Input Handling: Similar to the previous implementation, we read the input string and the word to be removed.
- Search for the Last Occurrence:
- We use
strstrin a loop to find the position of each occurrence of the word. - We keep updating the
lastPospointer to point to the most recent occurrence of the word.
- We use
- Remove the Word:
- If the word is found, we shift the characters to the left starting from the position after the word to the end of the string.
- This effectively removes the word from the string.
- Print the Result: We print the modified string. If the word is not found, we print a message indicating that the word was not found.