【问题标题】:Removing multiple blanks using putchar and getchar in C在 C 中使用 putchar 和 getchar 删除多个空白
【发布时间】:2014-11-29 17:41:50
【问题描述】:

问题:编写一个程序,使用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 上,程序在那里进入无限循环,而不是等待新字符。

【问题讨论】:

  • while(c == ' ') ; 是无限循环。更新c.

标签: c string getchar putchar


【解决方案1】:
#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() 中的最后一个红色字符。

【讨论】:

  • NMDV,但使用int c,所以EOF与所有256个不同的char区分开来。
  • main() -> int main(void) 。还可以添加 return 0; 以获得良好的风格。
【解决方案2】:

匿名者的回答有效,但有一个更简单的算法也有效:

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

在 C 中:

#include <stdio.h>

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

【讨论】:

  • 这并不完全符合我的要求。我给它输入了I am here to learn.,我得到了I m ere o earn.
【解决方案3】:
#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;
}

【讨论】:

    【解决方案4】:

    您的程序存在一些问题:

    • main() 的原型必须包含返回类型 int

    • c 必须定义为int,这样您才能正确区分EOFgetchar() 返回的所有有效字节值。

    • 识别出空白字符后,必须继续阅读字符并跳过后续的空白字符。

    • 从技术上讲,空白字符包括空格字符' ' 和制表符'\t'。您应该使用 &lt;ctype.h&gt; 中的 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;
    }
    

    【讨论】:

      【解决方案5】:

      这对我有用。

      #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;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-10-22
        • 1970-01-01
        • 1970-01-01
        • 2013-07-17
        • 2021-12-15
        相关资源
        最近更新 更多