#include <stdio.h>
#include <string.h>
void remove_last_occurrence(char *s, const char *word) {
char *p = s;
char *last_p = NULL;
// Find last occurrence
while ((p = strstr(p, word)) != NULL) {
last_p = p;
p++; // move forward to find next
}
if (last_p == NULL)
return; // word not found
int word_len = strlen(word) + 1;
int start = last_p - s; // index of last occurrence
// Shift characters left to remove the word
for (int j = start, i = start + word_len; s[i] != '\0'; i++, j++) {
s[j] = s[i];
}
// Null-terminate the shortened string
s[strlen(s) - word_len] = '\0';
}
int main() {
char s[50] = "c++ c python c++ java c++ php";
remove_last_occurrence(s, "c++");
puts(s);
}
/*
run:
c++ c python c++ java php
*/