#include #include #include #include // lookBehind(check, s_sub, word) removes any instance of s_sub // in word that is immediately preceded by check. // Requires: check, s_sub and word are non-NULL // Effects: modifies word void lookBehind(const char* check, const char* s_sub, char* word) { assert(check); assert(s_sub); assert(word); const size_t length_of_check = strlen(check); const size_t length_of_s_sub = strlen(s_sub); size_t length_of_word = strlen(word); // nothing to remove if (length_of_s_sub == 0) { return; } size_t start = length_of_check; while (start <= length_of_word && length_of_s_sub <= length_of_word - start) { if (strncmp(word + start, s_sub, length_of_s_sub) == 0 && strncmp(word + start - length_of_check, check, length_of_check) == 0) { // Shift the suffix left, including the '\0' terminator for (size_t i = start + length_of_s_sub; i <= length_of_word; ++i) { word[i - length_of_s_sub] = word[i]; } // Update the length of the word after removal length_of_word -= length_of_s_sub; // Check for new matches that start before the cut const size_t new_start = length_of_s_sub - 1; if (start - length_of_check >= new_start) { start -= new_start; } else { start = length_of_check; } } else { start++; } } }