user-0 提问: 关于删除主串中的子串的问题，想了一个下午，没找到方法。

回复:
user: user-1
content: C:\Program Files\Microsoft Visual Studio 10.0\VC\crt\src\memmove.c /*** *memmove.c - contains memmove routine * *       Copyright (c) Microsoft Corporation. All rights reserved. * *Purpose: *       memmove() copies a source memory buffer to a destination buffer. *       Overlapping buffers are treated specially, to avoid propogation. * *******************************************************************************/  #include <cruntime.h> #include <string.h>  /*** *memmove - Copy source buffer to destination buffer * *Purpose: *       memmove() copies a source memory buffer to a destination memory buffer. *       This routine recognize overlapping buffers to avoid propogation. *       For cases where propogation is not a problem, memcpy() can be used. * *Entry: *       void *dst = pointer to destination buffer *       const void *src = pointer to source buffer *       size_t count = number of bytes to copy * *Exit: *       Returns a pointer to the destination buffer * *Exceptions: *******************************************************************************/  void * __cdecl memmove (         void * dst,         const void * src,         size_t count         ) {         void * ret = dst;  #if defined (_M_IA64) || defined (_M_AMD64)          {           __declspec(dllimport)           void RtlMoveMemory( void *, const void *, size_t count );          RtlMoveMemory( dst, src, count );          }  #else  /* defined (_M_IA64) || defined (_M_AMD64) */         if (dst <= src || (char *)dst >= ((char *)src + count)) {                 /*                  * Non-Overlapping Buffers                  * copy from lower addresses to higher addresses                  */                 while (count--) {                         *(char *)dst = *(char *)src;                         dst = (char *)dst + 1;                         src = (char *)src + 1;                 }         }         else {                 /*                  * Overlapping Buffers                  * copy from higher addresses to lower addresses                  */                 dst = (char *)dst + count - 1;                 src = (char *)src + count - 1;                  while (count--) {                         *(char *)dst = *(char *)src;                         dst = (char *)dst - 1;                         src = (char *)src - 1;                 }         } #endif  /* defined (_M_IA64) || defined (_M_AMD64) */          return(ret); }

user: user-1
content: C:\Program Files\Microsoft Visual Studio 10.0\VC\crt\src\strstr.c /*** *strstr.c - search for one string inside another * *       Copyright (c) Microsoft Corporation. All rights reserved. * *Purpose: *       defines strstr() - search for one string inside another * *******************************************************************************/  #include <cruntime.h> #include <string.h>  /*** *char *strstr(string1, string2) - search for string2 in string1 * *Purpose: *       finds the first occurrence of string2 in string1 * *Entry: *       char *string1 - string to search in *       char *string2 - string to search for * *Exit: *       returns a pointer to the first occurrence of string2 in *       string1, or NULL if string2 does not occur in string1 * *Uses: * *Exceptions: * *******************************************************************************/  char * __cdecl strstr (         const char * str1,         const char * str2         ) {         char *cp = (char *) str1;         char *s1, *s2;          if ( !*str2 )             return((char *)str1);          while (*cp)         {                 s1 = cp;                 s2 = (char *) str2;                  while ( *s1 && *s2 && !(*s1-*s2) )                         s1++, s2++;                  if (!*s2)                         return(cp);                  cp++;         }          return(NULL);  }

user: user-0
content: 好像还是不行哦，还是高兴的太早了，那一段删除字符串怎么就搞不出来呢

user: user-0
content: 其中，CNP 没起到作用，忘记删除了。

user: user-0
content: /* Note:Your choice is C IDE */ #include "stdio.h" #include "string.h" char* ptr (char * p1, char * p2); int  del_substr ( char * st1, char * st2); int main() {	char * d; 	int cmp,cnp,cp,clp;  	char  a [100]= "zsdhfsj";  	char  b[100] = "sdhfsj";  	cnp = strlen(a);  	cp = strlen(b);  	d = ptr(a,b);  	cmp = del_substr(a,b);  	if(cmp)  	{  			for(clp = 0;clp<cp;clp++)  			{  				a[d -a] = a[d - a + cp];  				d++;	   			}  			printf("%s",a);			  	}  	else  	printf("没有任何子串");  	 } char * ptr (char * p1, char * p2) { 	char *str1,*str2; 	for(str1 = p1;*str1 != '\0';str1++) 		for(str2 = p2; *str2 != '\0';str2++) 			if(*str1 == *str2) 			return str1; 			 	return NULL; 	 } int del_substr( char * st1, char *st2) { 	char * temp; 	int i,count,p; 	count = 0; 	i = strlen(st2); 	if (st1 == NULL && st2 == NULL)	 		return 0; 	temp = ptr(st1 ,st2); 	if(temp == NULL) 		return 0; 	for(p = 0;p<i;p++) 		if(*temp++ == *st2++) 			count ++; 			if(i == count) 			return 1; 			 	return 0; 			 			 } 今天想了一下午，外加一晚上，才做出这个题目。是不是我不适合学C语言？ 没有用标准库函数

user: user-0
content: 不能用strlen函数。

user: user-2
content: 引用 3 楼 N_sev7 的回复:首先要找到子串在主串中的位置，然后删除 估计这题目是想考一下你怎么从主串中找到子串 子串匹配算法，建议看下KMP算法 +1  基本用KMP算法就可以了

user: user-3
content: 首先要找到子串在主串中的位置，然后删除 估计这题目是想考一下你怎么从主串中找到子串 子串匹配算法，建议看下KMP算法

user: user-1
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-4
content: 错了 if ('\n' == *(str+i)) 改为 if ('\0' == *(str+i))

user: user-4
content: int del_substr(char *str ,char const * substr) {     int iSubstrLen = 0;     int i = 0;     int j = 0;     if ((NULL == str) || (NULL == substr))     {         return 0;     }      iSubstrLen = strlen(substr);     while(1)     {         if ('\n' == *(str+i))         {             return 0;         }          if (*substr == *(str+i))         {             for (j = 0;j<iSubstrLen;j++)             {                 if ('\0' == *(substr+j))                 {                     return 1;                 }                  if (*(substr+j) != *(str+i+j))                 {                     j = iSubstrLen;                 }             }         }          i++;     }  }  未验证