【问题标题】:replace multiple blanks with one blank in C在C中用一个空格替换多个空格
【发布时间】:2013-05-30 23:19:04
【问题描述】:

我正在编写一些简单的 c 编程代码,用一个空格替换一串多个空格。我的代码如下,但显然包含错误。我试图避免使用数组或指针。那么关于如何纠正我的错误有什么建议吗?

#include <stdio.h>
int main(void)
{
    int c,d;
    d=0;
    while((c=getchar())!=EOF)
        {
        if (c==' ')
            {
                d=getchar();
                if (d!=' '&&d!=EOF)
                    putchar(c);
            }
        putchar(c);
        }
}

【问题讨论】:

    标签: c replace space


    【解决方案1】:

    首先:避免使用getcharputchar。请改用getcputc

    如果您非常想逐个字符地阅读,那么可以这样做:

    int c, lastc = '\0';
    while((c = getc(stdin)) != EOF) {
        if (c == ' ' && lastc == ' ')
            continue;
        putc(c, stdout);
        lastc = c;
    }
    

    【讨论】:

    • 哪里说getcharputchar 被弃用了?我在 N1570 的 7.21.7.6 或 7.21.7.8 中没有看到任何迹象。
    • 对不起,我的错误,将它们与确实已弃用的gets 混淆了。删除了那部分。
    • 我明白了。 gets 不仅已被弃用,它已被删除,天哪!
    • lastc 可以在未初始化的情况下使用。并且,请在新行中插入 continue;
    【解决方案2】:

    这段代码似乎运行良好:

    #include <stdio.h>
    
    int main(void)
    {
        int c, d;
        d = 0;
        while ((c = getchar()) != EOF)
        {
            if (c == ' ')
            {
                while ((d = getchar()) == ' ');
                putchar(c);
                putchar(d);
            } else {
                putchar(c);
            }
        }
    }
    

    【讨论】:

      【解决方案3】:

      在进行这种过滤时,最好不要重复读取输入,因为这往往会分散逻辑。至少根据我的经验,我发现如果我在一个地方读取输入然后处理它会更容易。

      对于这个问题,一个简单的状态机就足够了:只要记住最后一个字符是否为空白,如果最后读取的字符不是空白,则只输出空白。

      【讨论】:

        【解决方案4】:
        #include<stdio.h>
        
        int nextChar(FILE *fp){
            static int buff = 0;
            int ch = fgetc(fp);
            if(buff != ch)
                buff = ch;
            else
                while(buff == ' ')
                    buff = fgetc(fp);
            return buff;
        }
        
        int main(void){
            FILE *fp = stdin;
            int ch;
            while(EOF!=(ch=nextChar(fp))){
                putchar(ch);
            }
            return 0;
        }
        

        【讨论】:

          【解决方案5】:

          使用标志来记住您是否阅读了空白:

          #include <stdio.h>
          
          int main( void )
          {
            int lastBlank = 0;
            int c;
          
            while ( (c = fgetc( stdin )) != EOF )
            {
              if ( c != ' ' || !lastBlank )
                fputc( c, stdout );
          
              lastBlank = ( c == ' ' );
            }
            return 0;
          }
          

          【讨论】:

            【解决方案6】:
            # include <stdio.h>
            
            int main(){
            
              int c , n;
                n=0;
            
                while((c=getchar())!= EOF) {
                    if (c == '\t') {
            
                        if (n<1){
                            putchar('\t');
                            n=n+1;
                        }
                    }
                    else {
                        n=0;
                        putchar(c);
                    }
                }
            }
            

            【讨论】:

              【解决方案7】:
              #include <stdio.h>
              
              /*copy input to output, replacing each 
              string of one or more blanks by a single blank.*/
              
              main() {
              
                  int c;
              
                  while((c =getchar()) != EOF) {
                      if (c!= ' ') 
                          putchar(c);
                      else {
                          putchar(c);
                          while((c =getchar()) == ' ');
                          putchar(c);
                      }
                  }
              }
              

              【讨论】:

              • 一些解释: - 首先 while 循环检查是否没有 EOF 并继续操作 - 然后,“if”语句检查是否不存在空白字符并从输入流中打印字符 - 一次检测到空白字符,它进入 else 条件,然后不做任何事情它打印第一个空格 - 然后如果有多个空格,它进入 while 循环并且什么都不做,直到空格结束 - 当没有空格,它会输出并打印流中存在的下一个字符
              【解决方案8】:
              #include <stdio.h>
              
              int main() {
                  int c;
              
                  while((c = getchar()) != EOF) {
                      if (c != ' ')
                          putchar(c);
                      if (c == ' ') {
                          // if we found a blank continue reading
                          // searching for additional blanks
                          // put one blank char to output stream
                          // put the char that casue the while loop to exit
                          while((c = getchar()) == ' ');
                          putchar(' ');
                          putchar(c);
                      }
                  }
              }
              

              使用包含不同数量空白的简单文件进行测试会产生正确的行为

              ➜  course-c cat data
              one space two  spaces three   spaces four   spaces five     spaces
              
              
              ➜  course-c ./replace.blank < data
              one space two spaces three spaces four spaces five spaces
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2010-11-19
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2013-07-03
                • 2012-06-04
                相关资源
                最近更新 更多