user-0 提问: 用C语言编写一个函数，删除一个字符串的一部分

回复:
user: user-1
content: 不就是一个小小的函数吗，大家整那么复杂干啥。看朕的： int del_substr (char * str, char const * substr) {     int cnt = 0;     char * p, * q, * r;     while (q = (char *)substr, * (p = r = str ++) != '\0')         while (* q != '\0' && * p ++ == * q)             if (* ++ q == '\0' && ++ cnt)                 while ((* r ++ = * p ++) != '\0') ;     return cnt; }

user: user-2
content: 仅供参考：（我当初咋没给这四个字申请专利呢？） #include <stdio.h> #include <string.h> #include <locale.h> char s[256]; char *p; int r,n,i; int main() {     setlocale(LC_ALL,"chs");     while (1) {         printf("请输入一行文字(空行结束),\"%%20\"将替换为\" \",\"你懂得\"将替换为\"XXXXXX\":\n");         fgets(s,256,stdin);         if ('\n'==s[0]) break;         p=s;         while (1) {             p=strstr(p,"%20");             if (p) {                 memmove(p+1,p+3,strlen(p)-3+1);                 p[0]=' ';             } else break;         }         p=s;         while (1) {             p=strstr(p,"你懂得");             if (p) {                 memmove(p+6,p+6,strlen(p)-6+1);                 for (i=0;i<6;i++) p[i]='X';             } else break;         }         printf("%s",s);     }     return 0; } //请输入一行文字(空行结束),"%20"将替换为" ","你懂得"将替换为"XXXXXX": //abcdefg%20helloworld%20something.pdf //abcdefg helloworld something.pdf //请输入一行文字(空行结束),"%20"将替换为" ","你懂得"将替换为"XXXXXX": //这是测试文字你懂得，在这个你懂的地方，就得做你懂得的事 //这是测试文字XXXXX，在这个你懂的地方，就得做XXXXX的事 //请输入一行文字(空行结束),"%20"将替换为" ","你懂得"将替换为"XXXXXX": //

user: user-3
content: #include<stdio.h>  int del_substr(char *str,char const *substr); int index_of_str1(const char *str2,const char *str1); int count_len(const char *str);  //若str1在str2中，返回在str2中的位置，不然返回-1 int index_of_str1(const char *str2,const char *str1) { 	int index=0; 	if( count_len(str1) > count_len(str2) ) 		return -1;  		while(*str2 != '\0') 		{ 			int i = 0; 			if(*str1 == *str2) 			{	 				if( *(str1+1) == '\0') 					return index;  				if(*(str1+1) == *(str2+1) ) 					{ 						str1++,str2++; 						i++; 					} 				else 				{ 					str2++,index++; 				} 					 			} 			else 			{ 				str2++; 				str1-i; 				index++; 				if( count_len(str1) > count_len(str2) ) 				{ 					return -1; 				} 				 			} 				 		} }  int del_substr(char *str, char const *substr) { 	int i; 	int index = index_of_str1(str,substr); 	int str_len=count_len(str); 	int substr_len=count_len(substr);  	if( index >= 0 ) 	{	 		char *pstr=str; 		int num = str_len-substr_len+1;	 		char *newstr = malloc(sizeof(char)*num); 		char *tmp_str=newstr; 		 		for(i=0;i<index;i++) 		{ 			*tmp_str++=*pstr++;  		}  		pstr=pstr+substr_len; 		while(*pstr != '\0') 		{ 			*tmp_str++=*pstr++; 		}  		*(tmp_str)='\0';  		while(*newstr != '\0') 		{ 			*str++=*newstr++; 		} 		 		*str='\0';   		 		return 1; 	}  	return 0; }    //计算字符串的长度 int count_len(const char *str) { 	int len=0; 	while(*str != '\0') 	{ 		len++; 		str++; 	} 	return len; }  int main() { 	char str2[]="dededef"; 	char str1[]="de";  	printf("删除之前：%s\n",str2); 	del_substr(str2,str1); 	printf("删除之后：%s\n",str2); 	return 0; }

user: user-4
content: 当str为dededef，substr为de时，你的程序最后输出str应该为def,结果不对啊 引用 1 楼 user-7 的回复:int Strncmp(const char *cs, const char *ct, unsigned int count) {     unsigned char c1, c2;      while (count)     {         c1 = *cs++;         c2 = *ct++;         if (c1 != c2)             return c1 < c2 ? -1 : 1;         if (!c1)             break;         count--;     }     return 0; }  unsigned int Strlen(const char *s) {     const char *sc;      for (sc = s; *sc != '\0'; ++sc)         /* nothing */;     return sc - s; }  int del_substr(char *str, char const *substr) {     char *start = str;     char *index = str;     int len = Strlen(substr);      while(*index != '\0')     {         if(Strncmp(index, substr, len) == 0)             index += len;         *start++ = *index++;     }     *start = '\0'; } ,自己看看吧

user: user-4
content: 和二楼功能一样，差不多 引用 4 楼 starytx 的回复:仅供参考 // 计算字符串长度 size_t MyStrLen(const char *str) { 	size_t nLen = 0; 	while (*str++) 	{ 		nLen++; 	} 	return nLen; } // 返回src中子串substr开始的字符指针，找不到返回NULL  char * _FindStr( char *src, const char *substr) { 	size_t nSrcLen = MyStrLen( src); 	size_t nSubstrLen = MyStrLen(substr);  	if (nSrcLen < nSubstrLen) 	{ 		return NULL; 	} 	const char *p1 = src; 	char *pBegin = src; 	const char *p2 = substr; 	bool bFlag = false; 	while (*p1 && MyStrLen(p1) >= nSubstrLen && *p2) 	{ 		if (*p1 == *p2) 		{ 			p1++; 			p2++; 			bFlag = true; 		} 		else 		{ 			bFlag = false; 			pBegin++; 			p1 = pBegin; 			p2 = substr; 		} 	}  	if (bFlag) 	{ 		return pBegin; 	} 	return NULL; }  int del_substr( char *str ,const char *substr) { 	if (0 == str || 0 == substr ) 	{ 		return false; 	} 	char *pFind = _FindStr(str,substr); 	bool bFlag = false; 	while (pFind)		// 循环处理，以便删除后续再次出现的子串 	{ 		bFlag = true; 		const char * pRight = pFind + MyStrLen(substr); 		while (*pRight) 		{ 			*pFind++= *pRight++; 		} 		*pFind = 0; 		pFind = _FindStr(str,substr); 	} 	if (bFlag) 	{ 		return 1; 	}  	return 0; }  int main() {  	char str[] = "abcdefgcdet"; 	printf("原串：%s\n" ,str); 	char *substr = "cde"; 	if (del_substr(str,substr)) 	{ 		printf("删除%s后：%s\n",substr,str) ; 	} 	 	return 0; }

user: user-5
content: 仅供参考 // 计算字符串长度 size_t MyStrLen(const char *str) { 	size_t nLen = 0; 	while (*str++) 	{ 		nLen++; 	} 	return nLen; } // 返回src中子串substr开始的字符指针，找不到返回NULL  char * _FindStr( char *src, const char *substr) { 	size_t nSrcLen = MyStrLen( src); 	size_t nSubstrLen = MyStrLen(substr);  	if (nSrcLen < nSubstrLen) 	{ 		return NULL; 	} 	const char *p1 = src; 	char *pBegin = src; 	const char *p2 = substr; 	bool bFlag = false; 	while (*p1 && MyStrLen(p1) >= nSubstrLen && *p2) 	{ 		if (*p1 == *p2) 		{ 			p1++; 			p2++; 			bFlag = true; 		} 		else 		{ 			bFlag = false; 			pBegin++; 			p1 = pBegin; 			p2 = substr; 		} 	}  	if (bFlag) 	{ 		return pBegin; 	} 	return NULL; }  int del_substr( char *str ,const char *substr) { 	if (0 == str || 0 == substr ) 	{ 		return false; 	} 	char *pFind = _FindStr(str,substr); 	bool bFlag = false; 	while (pFind)		// 循环处理，以便删除后续再次出现的子串 	{ 		bFlag = true; 		const char * pRight = pFind + MyStrLen(substr); 		while (*pRight) 		{ 			*pFind++= *pRight++; 		} 		*pFind = 0; 		pFind = _FindStr(str,substr); 	} 	if (bFlag) 	{ 		return 1; 	}  	return 0; }  int main() {  	char str[] = "abcdefgcdet"; 	printf("原串：%s\n" ,str); 	char *substr = "cde"; 	if (del_substr(str,substr)) 	{ 		printf("删除%s后：%s\n",substr,str) ; 	} 	 	return 0; }

user: user-6
content: 家庭作业么。。。。

user: user-7
content: int Strncmp(const char *cs, const char *ct, unsigned int count) {     unsigned char c1, c2;      while (count)     {         c1 = *cs++;         c2 = *ct++;         if (c1 != c2)             return c1 < c2 ? -1 : 1;         if (!c1)             break;         count--;     }     return 0; }  unsigned int Strlen(const char *s) {     const char *sc;      for (sc = s; *sc != '\0'; ++sc)         /* nothing */;     return sc - s; }  int del_substr(char *str, char const *substr) {     char *start = str;     char *index = str;     int len = Strlen(substr);      while(*index != '\0')     {         if(Strncmp(index, substr, len) == 0)             index += len;         *start++ = *index++;     }     *start = '\0'; } ,自己看看吧