Ive been trying to remove all occurrences of a substring from a given string but my code has a bug which I don't know how to resolve. When two substrings are back-to-back my code can only remove one of them.
This is my code:
void removeSub(char *str, char *sub, char *new) {
int len = strlen(sub);
char *subp = strstr(str, sub);
int position = subp - str;
for (int i = 0; i <= strlen(str); i++) {
if (i >= position && i < position + len) {
continue;
}
if (i == position + len - 1) {
// update position
subp = strstr(str + i - 1, sub);
position = subp - str;
}
char ch = str[i];
strncat(new, &ch, 1);
}
}
a quick explanation of code: I copy the string char by char into another string(new) and whenever I see the substring along the way my code continues the loop and does nothing. when a substring is passed I relocate the next substring. (I know this isn't very efficient but I'm new and don't know many ways and functions)