使用putchar和getchar在C中去除多个空格

3
问题:编写一个程序,使用getchar()接收文本输入并输出字符串,已去除多余的空格。
以下是我编写的伪代码:
While each input character is received before reaching EOF, do the following:
     1) if character is non-blank, print it out
     2) otherwise:
         a. print out the blank
         b. do nothing untill the next non-blank character 
     3) if a non-blank character is reached, go back to 1)

我尝试按照以下方式实现算法:

#include <stdio.h>
/* replaces multiple blanks with a single blank */
main(){
    char c;
    while((c= getchar())!=EOF){
        if (c != ' ')
            putchar(c);
        else {
            putchar(c);
            while(c == ' ')
                ;
        }
    }   
}

当一个字符串包含空格时,它会停止运行。我不确定应该如何调试它。我认为问题出在我的第二个while循环中,程序陷入了死循环,而不是等待新的字符。


2
while(c == ' ') ; 是一个无限循环。更新 c - BLUEPIXY
5个回答

3
#include <stdio.h>
/* replaces multiple blanks with a single blank */
main(){
    int c; // thanx chux
    while((c= getchar())!=EOF){
        if (c != ' ')
            putchar(c);
        else {
            putchar(c);
            while((c= getchar())!=EOF)
                if (c!=' ')
                {
                    putchar(c);
                    break;
                }
        }
    }   
}

您的代码最后一次没有从标准输入中读取字符,导致了循环与前一个getchar()函数读取的最后一个字符无限比较的情况。

1
使用 int c 来区分所有 256 种不同的 char,从而区分 EOF。将 NMDV 改为这种方式。 - chux - Reinstate Monica
main() -> int main(void)。为了良好的风格,还需添加return 0; - chqrlie

2

匿名用户的答案是正确的,但还有一个更简单的算法也可以实现:

While there is input remaining:
    Read a character.
    If the current and previous characters aren't both blank:
        Print the current character.

In C:

#include <stdio.h>

int main() {
    int prev = EOF, c;
    while ((c = getchar()) != EOF) {
        if (c != ' ' || prev != ' ')
            putchar(c);
        prev = c;
    }
    return 0;
}

这并不完全符合我的要求。我输入了“我在这里学习。”,但得到的是“I m ere o earn.”。 - Omid

1
#include <stdio.h>

int main(void){
    int c;
    while((c = getchar())!=EOF){
        if (c != ' ')
            putchar(c);
        else {
            putchar(c);
            while((c = getchar()) == ' ')
                ;
            ungetc(c, stdin);//go back 1
        }
    }
    return 0;
}

0

这对我有用。

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int c;
    int space = 0;

    while ((c = getchar())!= EOF){

            if (c != ' '){
                putchar(c);
            }else{
                if(space == ' '){
                    continue;
                }else{
                    putchar(c);
                }
            }
            space = c;
    }
    return 0;
}

0
你的程序有几个问题:
1. `main()` 的原型必须包括返回类型 `int`。 2. `c` 必须被定义为 `int`,这样你可以正确区分 `getchar()` 返回的所有有效字节值和 `EOF`。 3. 在识别到空白字符后,你必须继续读取字符并跳过后续的空白字符。 4. 从技术上讲,空白字符包括空格字符 `' '` 和制表符字符 `'\t'`。你应该使用 `` 中的 `isblank()` 函数,并修改你的程序以跳过后续的空白字符。
下面是修改后的版本:
#include <ctype.h>
#include <stdio.h>

/* replaces multiple blanks with a single blank */
int main(void) {
    int c;
    while ((c = getchar()) != EOF) {
        putchar(c);
        if (isblank(c)) {
            while (isblank(c = getchar())
                continue;
            if (c == EOF)
                break;
            putchar(c);
        }
    }
    return 0;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接