|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
C语言,编写函数squeeze(s1,s2),将字符串s1中任何与字符串s2中匹配的字符都删除
程序如下,书上的题目,我先自己做了一遍感觉循环那出了问题,改了好多遍都没成功,自己百度搜索相关题目,感觉不是一种写法,对比之后还是检查不出来(水平有限,感觉我逻辑很差 ),请大佬们帮帮忙,看看程序问题出现在哪里。感激感激 #include <stdio.h>
void squeeze(char s1[], char s2[]);
int main()
{
char s1[] = {'a','b','c','d','e','f','g','h','i','k',0};
char s2[] = {'b','c','e','g','i',0};
squeeze(s1, s2);
printf("%s \n", s1);
return 0;
}
void squeeze(char s1[], char s2[])
{
int i, j, k;
i = j = k = 0;
for(i;s1[i]!='\0';i++)
{k++;
for(j;s2[j]!='\0';j++)
{
if(s1[i]!=s2[j])
s1[k]=s1[i];
else
s1[k]='\0';
}
}
}
#include <stdio.h>
void squeeze(char s1[], char s2[]);
int main()
{
char s1[] = {'a','b','c','d','e','f','g','h','i','k',0};
char s2[] = {'b','c','e','g','i',0};
squeeze(s1, s2);
printf("%s\n", s1);
return 0;
}
void squeeze(char s1[], char s2[])
{
for(int i = 0; s1[i] != '\0'; i++)
{
for(int j = 0; s2[j] != '\0'; j++)
{
if(s1[i] == s2[j])
{
for(int k = i; s1[k] != '\0'; k++)
s1[k] = s1[k+1];
i--;
break;
}
}
}
}
|
|