gpt4 book ai didi

c - 如何在c中读取字符串?

转载 作者:行者123 更新时间:2023-11-30 18:58:21 25 4
gpt4 key购买 nike

我使用此代码,但它不起作用。

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

int main() {
char c, *strx = 0;
c = getchar();
while(c != '\n') {
strx = strcat(strx, &c);
c = getchar();
}
printf("%s\n", *strx);
return 0;
}

如何将单词放入字符串中?

最佳答案

改变

char *strx = 0

char strx [256];

例如:

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

int main()
{
char c, strx[256] = "";
int i = 0;
c = getchar();
while (c != '\n') {
strx[i++] = c;
c = getchar();
}
printf("%s\n", strx);
return 0;
}

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

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