#include #include // determines whether or not the string `str` begins with the prefix `prefix`. // automatically returns false if `prefix` is longer than the string `str`. bool starts_with(const char *str, const char *prefix) { int prefix_len = strlen(prefix); // the string cannot start with a prefix that is longer than itself if (prefix_len > strlen(str)) { return false; } // compare character by character for (int i = 0; i < prefix_len; i++) { if (prefix[i] != str[i]) { return false; } } return true; } // removes all occurrences of a substring `s_sub` from the string `word` when // the substring `s_sub` is preceded by a substring `check`. the same process is // applied to all updated versions of word that occur due to removing `s_sub`. // // effects: mutates `word` void lookBehind(const char *check, const char *s_sub, char *word) { char *write_ptr = word; int word_len = strlen(word); int check_len = strlen(check); int s_sub_len = strlen(s_sub); // traverse entire string, including \0 at the end for (int i = 0; i < word_len + 1; i++) { // check if `word` starting from the index i character begins with `check` if (starts_with(word + i, check)) { // copy `check` to the output string, because it is not getting removed for (int j = 0; j < check_len; j++) { *write_ptr = *(word + i); write_ptr++; i++; } // skip any consecutive occurences of `s_sub` while (starts_with(word + i, s_sub)) { i += s_sub_len; } } // copy the current character to the outputted string *write_ptr = *(word + i); write_ptr++; } }