c精彩编程挑战高手13

来源:百度知道 编辑:UC知道 时间:2024/05/27 10:38:30
编写程序,将字符串a中的子字符串b删除,程序返回在a中删除的子字符串b的个数和新字符串a,设字符串不超过80字节,以最简代码编写,不超过20行。

我的代码和你的名字一样。20行,有点意思。
#include <stdio.h>
#include <string.h>
int main()
{ int i,j,len1,len2,total=0;
char a[81],b[81],*p;
printf("Please input string a and b:\n");
gets(a);
gets(b);
len1=strlen(a);
len2=strlen(b);
p=strstr(a,b);
while(p)
{ for(i=0;i<len1-(p-a);i++)
*(p+i)=*(p+i+len2);
*(p+i)='\0';
total++;
p=strstr(a, b);
len1-=len2;
}
printf("New string a is: %s\n",a);
printf("The times are: %d\n",total);
system("pause");
return 0;
}