删除子串

/*判断第二个字符串是否为第一字符串的子串,如果是,则将该子串删除 比如 bissrc = a123afed littlesrc = afe; 返回a123d; */ #include <stdio.h> #include <string.h> #include <stdlib.h> //判断一个字符串是否为另一个的子串,如果是,返回该子串首字母所在的位置 //如果不是,返回-1。 int findsubstring(char bigsrc[], const char littlesrc[]) { char *flagbig = bigsrc; char *flaglittle =littlesrc; char *pbig,*plittle; pbig = bigsrc; plittle = littlesrc; for (pbig = bigsrc; *pbig != '/0'; pbig++) { if (*pbig == *plittle) { flagbig = pbig; flaglittle = plittle; plittle++; flagbig++; if (*plittle == '/0') return (flagbig - (plittle - littlesrc) - bigsrc); } else plittle = littlesrc; if ((flagbig != bigsrc) && (*(pbig + 1) != *(flaglittle + 1))) { pbig = pbig - (flaglittle - littlesrc); flagbig = bigsrc; flaglittle = plittle; plittle = littlesrc; } } return -1; } //字符删除,将src中第loc个字符删除 int leespy(char src[],int loc) { int len = strlen(src); for (loc; loc < len; loc++) *(src + loc) = *(src + loc + 1); src[len] = '/0'; } //删除子串 int del_substr(char str[], const char substr[]) { int ret; int sublen = strlen(substr); int tmp; if (substr == NULL) { return 1; } ret = findsubstring(str, substr); tmp = ret; if (ret > 0) { for (ret; ret < tmp + sublen; ret++) //由于每次只能删除1个,所以用循环删除 leespy(str,tmp); return 1; } else return 0; } int main (int argc, char *argv[]) { char src[] = "a123afed"; char little[] = "afe"; del_substr(src,little); printf("%s/n",src); return(0); }

你可能感兴趣的:(null)