# include <stdio.h>
# define N 100
void squeeze(char s1[], char s2[]);
int main()
{
char s1[N];
char s2[N];
printf(“请输入两个字符串\n”);
gets(s1);
gets(s2);
squeeze(s1, s2);
printf(“%s”, s1);
return 0;
}
void squeeze(char s1[], char s2[])
{
int i, j, k;
k = 0;
for (i = 0; s1[i] != “”\0″”; i++)
for (j = 0; s2[j] != “”\0″”; j++)
{
if (s1[i] != s2[j])
s1[k] = s1[i];
k++;
}
}
那位高手帮本人看一下代码错哪了,该代码是为了实现将s1 s2两个字符串中相同的元素去除后输出s1
# define N 100
void squeeze(char s1[], char s2[]);
int main()
{
char s1[N];
char s2[N];
printf(“请输入两个字符串\n”);
gets(s1);
gets(s2);
squeeze(s1, s2);
printf(“%s”, s1);
return 0;
}
void squeeze(char s1[], char s2[])
{
int i, j, k;
k = 0;
for (i = 0; s1[i] != “”\0″”; i++)
for (j = 0; s2[j] != “”\0″”; j++)
{
if (s1[i] != s2[j])
s1[k] = s1[i];
k++;
}
}
那位高手帮本人看一下代码错哪了,该代码是为了实现将s1 s2两个字符串中相同的元素去除后输出s1
解决方案:20分
显然错了啊,没判断能否结束啊
void squeeze(char s1[], char s2[])
{
int i, j, k;
k = 0;
for (i = 0; s1[i] != “”\0″”; i++)
for (j = 0; s2[j] != “”\0″”; j++)
{
if (s1[i] == s2[j])
break;
}
if(s2[j] == “”\0″”)
{
s1[k] = s1[i];
k++;
}
}
void squeeze(char s1[], char s2[])
{
int i, j, k;
k = 0;
for (i = 0; s1[i] != “”\0″”; i++)
for (j = 0; s2[j] != “”\0″”; j++)
{
if (s1[i] == s2[j])
break;
}
if(s2[j] == “”\0″”)
{
s1[k] = s1[i];
k++;
}
}
解决方案:20分
void squeeze(char s1[], char s2[]) { int i, j, k; k = 0; for (i = 0; s1[i] != ""\0""; i++) { for (j = 0; s2[j] != ""\0""; j++) if (s1[i] == s2[j]) break; if(s2[j] == ""\0"") s1[k++] = s1[i]; } }