小白发问,这个C语言任务该如何编写?

2024-11-27 06:57:38
推荐回答(1个)
回答1:

void squeeze(char* str1, char* str2)
{
int len1 = strlen(str1);
int len2 = strlen(str2);
char* temp = (char*)malloc(len1);
memset(temp, 0, len1);
int tempLen = 0;
for (int i = 0; i < len1;++i)
{
char* p = find(str2, str2 + len2, str1[i]);
if (p == str2 + strlen(str2)) //在s2中没找到与s1[i],s1[i]就是有效字符
{
temp[tempLen++] = str1[i];
}
}
memcpy(str1, temp, tempLen + 1);//+1 是为了把结束符拷贝进去
}
main()
{
char s1[20] = "THISISABOOKS";
char s2[5] = "IS";
squeeze(s1, s2);
printf("%s", s1);
}