gpt4 book ai didi

c - 为什么这个 C 子字符串方法不起作用?

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

到目前为止我已经写好了。子串(原件,3,3,子串)鉴于:orig = "onetwothree"应该让substr = "two",但这不是我做错了什么?

void substring(char* source, int start, int length, char* target) {
int i;
for(i = 0; i < length; i++) {
target[i] = source[start + i];
}
}

最佳答案

您忘记了对目标字符串进行零终结。

还有一些您应该实现的额外检查。例如,当源字符串在 length 个字符被复制之前结束时会发生什么?

此外,对源指针 (const char* source) 进行 const 限定并使用 size_t 而不是 int 来表示是有意义的索引和长度。

关于c - 为什么这个 C 子字符串方法不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20438722/

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