gpt4 book ai didi

c - K&R Exercise Squeeze 函数

转载 作者:太空宇宙 更新时间:2023-11-04 07:26:41 25 4
gpt4 key购买 nike

<分区>

所以这里的练习是设计一个程序,它接受一个字符串并删除该字符串中出现在第二个字符串中的所有字符。因此,对于我在下面选择的字符串,其中第一个字符串是 abc,第二个字符串是 cde,我想获得 ab 而不是 abc 的输出。

我已经看到了一种非常简洁的方法来执行此压缩功能,它只需要两个简单的 for 循环,但我想知道为什么我冗长的方法不起作用。

#include<stdio.h>

void squeeze(char s1[], char s2[]);
void copy(char to[], char from[]);

int k=0;

main()
{
char array1[4]="abc";
char array2[4]="cde";
squeeze(array1, array2);
printf("%s", array1);
}

void squeeze(char s1[], char s2[])
{
int j,i,m;
m=j=i=0;
char s3[1000];
while(s1[i]!='\0') //What I'm doing here is taking a character in the string
{ //and comparing it with all characters in the second string.
copy(s3,s1); //If it exists in the second string I'm 'deleting' that letter
while(s2[j]!='\0') //from the first string. Then I start all over again with the
{ // next letter in line. Or at least that's the plan.
if (s1[i]==s2[j])
{
k=1;
}
++j;
}

if (k==1)
{
m=i;
while(s3[m+1]!='\0')
{
s1[m]=s3[m+1]; //I'm 'deleting' the letter by pushing each character
++m; //that is to the right of the deleted character to the
} //left of the array.
}

if(k!=1)
{
++i;
}
}
s1[i]='\0';
}

void copy(char to[], char from[])
{
int i;
i=0;

while(from[i]!='\0')
{
to[i]= from[i];
++i;
}
to[i]=='\0';
}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com