思路就是标志位,两个状态之间切换

具体代码如下

 1 #include<stdio.h>
 2 
 3 void main()
 4 {
 5     long nc = 0;
 6     int spaceStatus = 0;
 7     int c = 0;
 8     while ((c = getchar()) != EOF)
 9     {
10         if (c == ' ')
11         {
12             if (spaceStatus == 0)
13             {
14                 spaceStatus = 1;
15                 putchar(c);
16             }
17 
18         }
19         if (c != ' ')
20         {
21             spaceStatus = 0;
22             putchar(c);
23         }
24     }
25 }

测试结果如下

C语言-将输入的字符输出并将多个空格按照一个空格的方式输出

 

相关文章:

  • 2021-03-31
  • 2021-12-29
  • 2021-06-09
  • 2021-11-08
  • 2021-10-13
  • 2022-12-23
猜你喜欢
  • 2021-08-26
  • 2021-10-19
  • 2022-12-23
  • 2022-12-23
  • 2021-05-28
  • 2022-12-23
  • 2021-10-18
相关资源
相似解决方案