C程序删除多个空格

问题描述 投票:0回答:1

我对以下程序有问题。它有效,但即使逻辑看起来正确,如果我插入由 m 空格分隔的两个字符,它会返回由 m/2 空格分隔的相同的两个字符。

#include <stdio.h>                                                                                                                                                                                                                                   
                                                                                                                                                                                                                                                     
int main(void)                                                                                                                                                                                                                                       
{                                                                                                                                                                                                                                                    
  int c;                                                                                                                                                                                                                                             
  int n = 0;                                                                                                                                                                                                                                         
                                                                                                                                                                                                                                                     
  printf("Insert one or more strings. Possible double blank spaces will be removed.\n\n");                                                                                                                                                           
                                                                                                                                                                                                                                                     
  while((c = getchar()) != EOF)                                                                                                                                                                                                                      
  {                                                                                                                                                                                                                                                  
    if(c != ' ')                                                                                                                                                                                                                                     
    {                                                                                                                                                                                                                                                
      if(n != 0)                                                                                                                                                                                                                                     
      {                                                                                                                                                                                                                                              
        putchar(c);                                                                                                                                                                                                                                  
        n = 0;                                                                                                                                                                                                                                       
      }                                                                                                                                                                                                                                              
      else                                                                                                                                                                                                                                           
      {                                                                                                                                                                                                                                              
        putchar(c);                                                                                                                                                                                                                                  
      }                                                                                                                                                                                                                                              
    }                                                                                                                                                                                                                                                
    else                                                                                                                                                                                                                                             
    {                                                                                                                                                                                                                                                
      if(n != 0)                                                                                                                                                                                                                                     
      {                                                                                                                                                                                                                                              
        n = 0;                                                                                                                                                                                                                                       
      }                                                                                                                                                                                                                                              
      else                                                                                                                                                                                                                                           
      {                                                                                                                                                                                                                                              
        putchar(c);                                                                                                                                                                                                                                  
        n++;                                                                                                                                                                                                                                         
      }                                                                                                                                                                                                                                              
                                                                                                                                                                                                                                                     
    }                                                                                                                                                                                                                                                
  }                                                                                                                                                                                                                                                  
  return 0;                                                                                                                                                                                                                                          
}

有人看到问题了吗?

c logic
1个回答
0
投票

正如几条评论中所指出的,代码重置标志变量

n
,有效地仅抑制任何一对连续SP中的第二个。


与其添加更多标志和条件,不如去散步并重新思考你的方法。

使用较少的代码,以下效果很好:

#include <stdio.h>

int main( void ) {
    puts( "Insert one or more strings. Consecutive blank spaces will be reduced to single spaces.\n" );

    for( int c, n = 0; (c = getchar()) != EOF; n = c == ' ' )
        if( !n || c != ' ' )
            putchar( c );

    return 0;
}
© www.soinside.com 2019 - 2024. All rights reserved.