user-0 提问: 字符串删除子串问题，研究了好几天还是搞不定，只好求助了

回复:
user: user-1
content: cstring str; str.replace("substr", "");

user: user-2
content: #include <stdio.h> #include <string.h>  /*              s1[] = "0123456789ABCDEF";  s2[] = "45678";   n1 = strlen(s1);  n2 = strlen(s2);  p1 = strstr(s1, s2);  p2 = p1 + n2;           n1          |  |---------------|  "0123456789ABCDEF"       |___||_____|       | |  |  |      p1 |  p2 |         |     |         n2    |              (n1-n2-(p1-s1))  */  int main(int argc, char *argv[]) {     char s1[] = "0123456789ABCDEF";     char s2[] = "45678";     char *p1;     char *p2;     int n1;     int n2;      n1 = strlen(s1);     n2 = strlen(s2);     p1 = strstr(s1, s2);     p2 = p1 + n2;          if (p1 != NULL)         memmove(p1, p2, n1 - n2 - (p1 - s1) + 1);      printf("%d\n",  n1 - n2 - (p1 - s1) + 1);     printf("s1=%s\n", s1);      return 0; } /* 输出: 8 s1=01239ABCDEF */

user: user-2
content: 用strstr找到子串，然后用memove删除即可  #include <stdio.h> #include <string.h>  /*              s1[] = "0123456789ABCDEF";  s2[] = "45678";   n1 = strlen(s1);  n2 = strlen(s2);  p1 = strstr(s1, s2);  p2 = p1 + n2;           n1          |  |---------------|  "0123456789ABCDEF"       |___||_____|       | |  |  |      p1 |  p2 |         |     |         n2    |              (n1-n2-(p-s1))  */  int main(int argc, char *argv[]) {     char s1[] = "0123456789ABCDEF";     char s2[] = "4567";     char *p1;     char *p2;     int n1;     int n2;      n1 = strlen(s1);     n2 = strlen(s2);     p1 = strstr(s1, s2);     p2 = p1 + n2;          if (p1 != NULL)         memmove(p1, p2, n1 - n2 - (p1 - s1) + 1);      printf("%d\n",  n1 - n2 - (p1 - s1) + 1);     printf("s1=%s\n", s1);      return 0; }

user: user-3
content: 拼音联想惹的祸。

user: user-4
content: 引用 4 楼 zhao4zhong1 的回复:仅供参考：#include <stdio.h> #include <string.h> char s[256]; char *p; int r,n,i; int main() {     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> #include <string.h> char s[256]; char *p; int r,n,i; int main() {     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-5
content: 咋研究的？？有没有单步调试？一边调试一边分析自己写的，肯定可以弄出来

user: user-6
content: //********************************************************************************* //【功能】在给定字符串中查找所有特定子串并删除，如果没有找到相应子串，则不作任何操作。 //【输入】 str：输入的被操作字符串 //              sub_str：需要查找并删除的特定子字符串 //【输出】 result_str：在str字符串中删除所有sub_str子字符串后的结果 //【返回】 删除的子字符串的个数 //【示例】输入：str = "abcde123abcd123"        sub_str = "123" //               输出：result_str = "abcdeabcd"  //              返回：2 //******************************************************************************** #include <iostream> #include <stdlib.h> #include <stdio.h> #include <string.h>  int delete_sub_str(const char *str, const char *sub_str, char *result_str) { 	int i=0,num=0; 	int sub_str_len=strlen(sub_str); 	const char *temp; 	const char *p,*p1; 	char *q; 	p=str; p1=sub_str; 	q=result_str; 	while(*str) 	{ 		if(*str==*sub_str)	 		{ 			temp=str;             //刚开始写成了while((*str++)==(*sub_str++)&&(*sub_str))，这样就出了问题，二者的不通过在于++之后判断*sub_str为空还是++之前 			while((*sub_str)&&(*str++)==(*sub_str++))  				i++; 			if(i==sub_str_len)  				num++; 			else 				for(int j=0;j<i;j++) 				{ 					*q++=*(temp+j); 				} 			sub_str=p1; 			i=0; 		} 		else 			*q++=*str++; 	} 	printf("%s\n",result_str); 	return num; } int main() { 	char *a="a123bc123de123abc123d123"; 	char *b="123"; 	char c[100]={0}; 	int i=delete_sub_str(a, b,c); 	printf("Total %d stings was deleted!\n",i); 	system("pause"); 	return 0; }

user: user-4
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; }  http://bbs.csdn.net/topics/390946225