我是练习代码。问题是,用新行替换输入中的空格。
我写了这样的内容:
int input;
while((input = getchar()) != EOF)
{
if (input == ' ')
{
input = '\n';
}
putchar(input);
}
但我不知道如何让它将多个空格更改为一个新行。我有一个想法,制作类似缓冲区变量(例如 int buffer)并在其中存储空间,然后检查输入后前一个字符是否为空格,但我不知道如何使其工作:P
最佳答案
int input, last_was_space = 0;
while((input = getchar()) != EOF)
{
if(input == ' ')
{
last_was_space = 1;
}
else
{
if(last_was_space)
{
last_was_space = 0;
putchar('\n');
}
putchar(input);
}
}
关于c - 用一个新行替换一个或多个空格。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43900246/