//编写函数squeeze(s1,s2),将字符串s1中任何与字符串s2匹配的字符都删去
#include <stdio.h>
#include <string.h>
char *squeeze(char*s1,char*s2);
char s1[];
char s2[];
char *s3;
int main()
{
int n=0;
s1="zhangchaozhang";
s2="chao";
printf("s1:%s\n",s1);printf("s2:%s\n",s2);
s3=squeeze(s1,s2);
printf("delete characters in s1 that matches s2 get s3:\n%s",s3);
}
char *squeeze(char*s1,char*s2)
{
int i,j,len1,len2;
int compare=1;//默认为1
int k=0;
char s3[100];
len1=strlen(s1);len2=strlen(s2);
for(i=0,k=0;i<len1;i++,k++)
{
if(s1[i]==s2[0])//s1遇到与s2第一个字符相等的字符,进入判断
{
for(j=i+1;(j<i+len2)&&(j<len1);j++)
{
if(s1[j]==s2[j-i])
compare=0;
else
{
compare=1;//有不相等的字符
break;
}
}
}
if(!compare)//s1中有和s2匹配的字符串
{
s3[k]=s1[i+len2];
}
else
s3[k]=s1[i];
}
s3[k]='\0';
return s3;
}