c - 如何在 Ubuntu 上给出 EOF,Ctrl-D 似乎不起作用?

标签 c linux ubuntu

我编写了以下代码,如果有多个空格,则仅打印一个空格。

#include<stdio.h>

void main()
{
    int c;
    while((c=getchar())!=EOF)
    {
        if(c==' '||c=='\t')
        {
            while(c==' '||c=='\t')
                c=getchar();
            putchar(' ');
            putchar(c);

        }   
        else
            putchar(c);
    }
}

最佳答案

您需要在 Enter 之后单独键入 Ctrl+D
或连续两次 Ctrl+D

./a.out
foo            bar<ENTER>
<Ctrl+D>

./a.out
foo         bar<Ctrl+D><Ctrl+D>

关于c - 如何在 Ubuntu 上给出 EOF,Ctrl-D 似乎不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35817420/

相关文章:

linux - 无法为 Ubuntu 安装 LightTable

ubuntu 上的脚本

php - 无法找到软件包 php5-redis Ubuntu 14.04

c - 非二进制补码平台上 char 中的 ASCII 代码表示

混淆错误与 opencv cvLoadImage 和 netbeans

c - epoll 和发送滞后

python - Linux 终端 : nicely formatted -h output

bash - 如何随机化列表并遍历随机化列表 (bash)

c - 需要解释 K&R fahr-to-cels 示例的组装说明

c++ - 平铺单纯形噪声?