Original question:
Write a program to copy its input to its output, replacing each string of one or more blanks by a single blank.
The first type:
This is most commonly used to set a inspace as a Boolean variable that flags whether the current input is in a character, or outside of a character
#include <stdio.h> int main (void) { int c; int inspace=0; while ((c = GetChar ()) = EOF) { if (c = = ") { if (InSpace = = 0) { inspace = 1; Putchar (c); } } else { inspace = 0; Putchar (c); } } return 0;}
The second kind: own thought, although the effect is similar, but the mentality is different
Number as the flag bit, record the amount of space
#include < stdio.h > main (void) { int c; int number; Number = 0; Initialization space Numbers while ((c = GetChar ()) = EOF) { if (c = = ') { ++number; if (number = = 1) Putchar (c); } if (c! = ") { Putchar (c); Number = 0; } } return 0; }
The third type:
Use a while loop to "take" multiple spaces
#include < stdio.h >main (void) { int c; while ((c = GetChar ()) = EOF) { if (' = = c) { Putchar (c); while ((c = GetChar ()) = = "&& c! = EOF) ; } if (EOF = = c) break ; Putchar (c); } return 0;}
The fourth type:
Use a character pc as the character before the "current character" to determine whether the trailing space is output by the PC
#include <stdio.h>int main () { int c, PC; PC = EOF; while ((c = GetChar ()) = EOF) { if (c = = ") if (PC! =") Putchar (c); if (c! = ") Putchar (c); PC = C; } return 0;}
C Language K&r Exercise series-Four ways to replace multiple spaces with a single space in a sentence