gpt4 book ai didi

c - 使两个数组长度相同。中(89)

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

传入的两个数组是常量,所以我新建了两个数组。第一个数组存储一组字符,第二个数组存储第二组字符。到目前为止,我假设第一组比第二组大。 (a、b、c、d > x、y)。该程序希望完成的是创建两个包含相同字母但较短的数组的新数组,在这种情况下 arr2 (newarr2) 重复最后一个字符,直到它与第一个数组的长度匹配。正确解决方案的例子。(a,b,c,d < x,y) --> equate_arr --> (a,b,c,d = x,y,y,y)

void equate_arr(char arg2[], char arg1[]){
size_t i = 0;
size_t len1 = strlen(arg1);
size_t len2 = strlen(arg2);
char newarr2[512];
char newarr1[512];

while(i < (strlen2 - 1))
{
newarr2[i] = arg2[i];
i++;
}

i = 0;

while(i < (strlen1 - 1))
{
newarr1[i] = arg1[i];
i++;
}

i = 0;

while(strlen(newarr2) < strlen(newarr1))
{
newarr2[strlen(newarr2)] = newarr2[strlen(newarr2)-1]
}
}

目前我不知道发生了什么,因为一旦我在我的代码中修改了这个函数,程序似乎就不再运行了。很抱歉询问我正在做的这个项目,但我确实需要一些帮助。

如果需要,我可以把整个程序放在这里。

已修改

void tr_non_eq(char arg1[], char arg2[], int len1, int len2)
{
int i = 0;
char* arr2;
arr2 = (char*)calloc(len1+1,sizeof(char));
while(i < len2)
{
arr2[i] = arg2[i];
i++;
}

while(len2 < len1)
{
arr2[len2] = arg2[len2-1];
len2++;
}

tr_str(arg1, arr2);
}

现在使用输入 (a,b,c,d,e,f) 和 (x,y) 和一个字符串“cabbage”来翻译程序打印出“yxyyx”并打印出字符串“abcdef” “xyy”表示 promise 。我不太清楚为什么 arr2 数组没有按预期填充“y”字符。

最佳答案

正如 de-duplicator 所说,就您的代码而言,它实际上什么也做不了。更重要的是,它试图做的事情充满了危险。

您使用 strlen 的事实确定你的论点的长度是一个明确的指标 equate_arr不希望收到两个 char 数组.相反,它需要两个以 NUL 结尾的 C 风格字符串。所以声明应该更像是:

void equate_arr(const char *arg2, const char *arg1)

这让合约更清晰一些。

但请注意返回类型:void .这表示您的函数不会向调用者返回任何值。那么,您打算如何返回修改后的数组?

下一个危险在于这些行:

char newarr2[512];
char newarr1[512];

如果使用大于 511 个字符(加上 NUL)的字符串调用此函数,会发生什么情况? “缓冲区溢出”这个短语应该在这里跳出来。

你需要的是malloc缓冲区大到足以容纳传入的最长字符串的副本。但这提出了一个问题,即如何将新数组交还给调用者(还记得 void 返回类型吗?)。

这里还有许多其他问题,主要归结为没有明确定义此功能要满足的契约(Contract)。

在我仔细观察的时候再看一个

while(strlen(newarr2) < strlen(newarr1))
{
newarr2[strlen(newarr2)] = newarr2[strlen(newarr2)-1]
}

第一次通过此循环会覆盖 newarr2 中的终止 NUL ,这意味着下一次调用 strlen陷入未定义的行为,因为它完全受堆栈中任何垃圾的支配。

如果您不清楚 C 风格的字符串,请查看我对 this 的回答详细介绍了它们的问题。

以下是白板代码(即未编译、未测试),它可以完成您想要实现的目标。仅供引用

    // Pad a string so that it is the same length as another. Padding is done
// by replicating the final character.
//
// @param padThis: A C-style string in a non-constant buffer.
// @param bufLength: The size of the buffer containing padThis
// @param toMatchThis: A (possibly) const C-style string to act
// as a template for length
//
// Pre-conditions:
// - Both padThis and toMatchThis reference NUL-terminated sequences
// of chars
// - strlen(padThis) < bufLength. Violating this will exit the program.
// - strlen(toMatchThis) < bufLength. If not, padThis will be padded
// to bufLength characters.
//
// Post-conditons:
// - The string referenced by toMatchThis is unchanged
// - The original string at padThis has been padded if necessary to
// min(bufLength, strlen(toMatchThis))

void padString(char * padThis, size_t bufLength, const char * toMatchThis)
{
size_t targetLength = strlen(toMatchThis);
size_t originalLength = strlen(padThis);

if (originalLength >= bufLength)
{
fprintf(stderr, "padString called with an original which is longer than the buffer!\n");
exit(EXIT_FAILURE);
}


if (targetLength >= bufLength)
targetLength = bufLength -1; // Just pad until buffer full

if (targetLength <= strlen(padThis))
return; // Nothing to do

// At this point, we know that some padding needs to occur, and
// that the buffer is large enough (assuming the caller is not
// lying to us).

char padChar = padThis[originalLength-1];
size_t index = originalLength;
while (index < targetLength)
padThis[index++] = padChar;
padThis[index] = '\0';
}

关于c - 使两个数组长度相同。中(89),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28388395/

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