题目中给你的实例可不足以用来做测试数据。
你那个代码试一下下面的两个很简单数据就找到错误了:
aaa
a
aaa
aaa
我自己写一个试试~~
你那个代码试一下下面的两个很简单数据就找到错误了:
aaa
a
aaa
aaa
我自己写一个试试~~
#include <stdio.h> #include <string.h> #include <stdlib.h> int main(void) { char str[501], sub[501]; int rLen, bLen, i; gets(str); gets(sub); rLen = strlen(str); bLen = strlen(sub); for(i = 0; i <= rLen - bLen; ) { if(!strncmp(str + i, sub, bLen)) { memmove(str + i, str + i + bLen, strlen(str + i + bLen)); *(str + (rLen -= bLen)) = '\0'; i = 0; } else i++; } puts(str); system("pause"); return 0; }
#include <stdio.h> #include <string.h> int locate(char * s, char *ss) { int i = 0, j = strlen(s) - strlen(ss) + 1; for (i = 0; i < j; i++) { if (memcmp(&s[i], ss, strlen(ss)) == 0) { return i; } } return -1; } int main(int argc, char* argv[]) { char s[501] = {0}, ss[501] = {0}; int i = 0; gets(s); gets(ss); while ((i = locate(s, ss)) != -1) { s[i] = 0; strcat(s, &s[i + strlen(ss)]); } printf("%s\n", s); return 0; }
#include<stdio.h> #include <string.h> char src[500] = {0}; char sub[500] = {0}; char res[500] = {0}; int main() { while(NULL != gets(src)) { gets(sub); char *pos = NULL; int sublen = strlen(sub); while(NULL != strstr(src,sub)) { int i = 0; char *r = res; while(NULL != (pos = strstr(src+i,sub))) { int len = pos - (src+i); strncpy(r,src+i,len); r += len; i += (len+sublen); } strcat(res,src+i); memset(src,0,sizeof(src)); strncpy(src,res,strlen(res)); memset(res,0,sizeof(res)); } puts(src); } return 0; }这个可以AC