C language programming Second Edition
1. Write a program that copies the input to the output, and replace multiple spaces with one space?
View the Code directly:
For reference only. The code is from the Internet !!!
Code 1:
1 #include "stdio.h"
2
3 main()
4 {
5 int c;
6 int i;
7 int n = 0;
8
9 while ( (c = getchar()) != EOF)
10 {
11 if ( c != '' )
12 {
13 putchar(c);
14 }
15 else if ( n != '')
16 {
17 putchar(c);
18 }
19
20 n = c;
21 }
22 }
Code 2 (detailed ):
1 #include <stdio.h>
2
3 /* count lines in input */
4 int
5 main()
6 {
7 int c, pc; /* c = character, pc = previous character */
8
9 /* set pc to a value that wouldn't match any character, in case
10 this program is ever modified to get rid of multiples of other
11 characters */
12
13 pc = EOF;
14
15 while ((c = getchar()) != EOF) {
16 if (c == '')
17 if (pc != '') /* or if (pc != c) */
18 putchar(c);
19
20 /* We haven't met 'else' yet, so we have to be a little clumsy */
21 if (c != '')
22 putchar(c);
23 pc = c;
24 }
25
26 return 0;
27 }
PS: My eyes burst into tears ~~ O (>_<) o ~~