Function prototype:
int del_substr(char *str,char const *substr);
First, you should judge whether substr is in Str. If not, return 0;
If so, the function should copy all the characters behind the substring in STR to the position of the substring, delete the substring, and return 1. If the substr appears in STR multiple times, the function only deletes the substring that appears for the first time. The second parameter of the function should never be modified.
Source code:
Int del_substr (char * STR, char const * substr) {If (STR = NULL | substr = NULL) {return 0 ;} // mark the start position of substr in STR as char * begin = STR; // mark the end position of substr in STR as char * end = STR; // substr cursor pointer char const * Index = substr; while (* begin! = '\ 0' & * end! = '\ 0') {// search for substrwhile (* begin! = * Index) {begin ++;} End = begin + 1; index ++; while (* Index = * end & * index! = '\ 0' & * end! = '\ 0') {index ++; end ++ ;}// if both index and end reach the end of the respective string, the substr substring is found in Str. // For example, STR: "abcdexyz", substr: "XYZ" If (* Index = '\ 0' & * end =' \ 0') {* begin = '\ 0'; printf (STR ); return 1;}/* If the index reaches the end of The substr string, but the end does not reach the end of the STR, The substr substring is found in Str * For example, STR: "abcdexyzmn", substr: "XYZ" */else if (* Index = '\ 0' & * end! = '\ 0') {While (* begin ++ = * end ++); printf (STR); return 1 ;}/ * If the index does not reach the end of the substr string, the end has reached the end of STR, and the substr substring is not found in Str * For example, STR: "abcdexyzmn", substr: "xyzmnst" */else if (* index! = '\ 0' & * end =' \ 0') {printf (STR); Return 0;}/* string matching has not been completed * For example, STR: "xyzabcdexyzmnstghd ", substr: "xyzmnst" */else {begin ++; end = begin; Index = substr ;}} printf (STR); Return 0 ;}
If you need to delete all substr in STR that contains multiple substr, you can use the above function for recursive calling.