gpt4 book ai didi

c - 如何在C中收集字符?

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

我想从像 a,c,e,f 这样的字符串中收集所有字母,并将它们放入另一个字符串 acef 中。我得到了一个困惑的结果,acef■ ┌sa,c,e,f。这是我的代码:

#include <stdio.h>
#include <string.h>

int main() {
int i,j=0;
char str[10];
strcpy(str,"a,c,e,f");
char tmp[10];
for(i=0;i<strlen(str);i++) {
if(str[i]==',') {
continue;
}
else {
tmp[j++]=str[i];
}
}
printf(" %s",tmp);
return 0;
}

最佳答案

char tmp[10]; ... printf("%s",tmp); 缺少某个空字符

您需要空终止符。

tmp[j] = '\0'; // after for-loop. 

@BLUEPIXY

关于c - 如何在C中收集字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44579850/

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