gpt4 book ai didi

c - 如何在 C 中逐字获取输入而不获取整个字符串?

转载 作者:行者123 更新时间:2023-11-30 20:58:37 27 4
gpt4 key购买 nike

假设我有一个段落作为这样的输入

C is an imperative procedural language. It was designed to be compiled using a relatively straightforward compiler, to provide low-level access to memory, to provide language constructs that map efficiently to machine instructions, and to require minimal run-time support. Despite its low-level capabilities, the language was designed to encourage cross-platform programming

如何在不获取整个段落的情况下逐字获取输入?

最佳答案

当用户按下回车键时,输入流将保存整个输入 block 以及回车符。从那里,只需根据您的喜好阅读或格式化它即可。

就您而言,只要您不关心标点符号,strtok 就可以正常工作。

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

int main(void) {

char Stream[1024] = {0};
char* TokenPointer = 0;
const char Token[] = " ";

fgets(Stream,sizeof(Stream),stdin);
TokenPointer = strtok(Stream, Token);
while(TokenPointer != 0)
{
puts(TokenPointer);
TokenPointer = strtok(0,Token);
}
return 0;
}

关于c - 如何在 C 中逐字获取输入而不获取整个字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51423657/

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