小编har*_*ski的帖子

为什么我需要在这段代码中显式地 putchar(' ') ?

我正在通过 Kernighan & Ritchie 进行工作,并且必须练习 1.9。事实上,我编写了一些似乎可以解决这个练习的代码,并且我已经在 Windows(使用 Git Bash 和 gcc)和 Termux(使用 clang)上测试了它,方法是在一行中输入可变数量的空格,例如 ,echo " one two three"以及预期的输出出来即 one two three

我通过反复试验找到了解决方案,尽管它与 Lvictor 在clc wiki上提供的解决方案相同。

我自己写的代码是:

#include <stdio.h>
    
/* Write a program to copy its input to its output, replacing each string of one or more blanks by a single blank. */
    
int main () {
    int c;
    
    while ((c = getchar()) != EOF) {
        if (c == ' ') {
            while ((c = getchar()) == …
Run Code Online (Sandbox Code Playgroud)

c kernighan-and-ritchie

1
推荐指数
1
解决办法
89
查看次数

标签 统计

c ×1

kernighan-and-ritchie ×1