gpt4 book ai didi

将字符数组的一部分复制到C中的另一个数组

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

所以我一直试图解决以下问题无济于事:

Write a function called removeString to remove a specified number of characters from a character string. The function should take three arguments: the source string, the starting index number in the source string, and the number of characters to remove. So, if the array text contains the string "the wrong son", the call

removeString (text, 4, 6);

has the effect of removing the characters "wrong " (the word "wrong" plus the space that follows) from the array text. The resulting string inside text is then "the son".

我已经尝试查找其他解决方案,但所有解决方案都使用了我想避免使用的 memcpy 函数,因为我的书中还没有介绍它,我宁愿不这样做可以这么说,“欺骗系统”。

我的removeString函数如下:

void removeString (char text[], int x, int y)
{
char output[81];
int i, z = 0;

for ( i = 0; (i <= 81); i++) {

if (z < (x-1) || z > (x+y-1) ) {
output[z] = text[z];
//printf("%i\n", z);
z++;
} else {
z++;
}

}

}

int main(void)
{
char s1[81] = "the wrong son";
int startPoint = 4, runLength = 6;

removeString(s1, startPoint, runLength);

printf("The new text is as follows:\n");
printf("%s\n", s1);

return 0;
}

当我打印出“z”的值时,我看到它在跳过数字,但出于某种原因,它看起来像是将所有 text[] 复制到 output[].

最佳答案

这将对原始字符串起作用

for (;text[x];x++)
{
text[x]=text[x+y];
}

关于将字符数组的一部分复制到C中的另一个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19795704/

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