c语言:编写一个将输入复制到输出的程序,并将其中的多个空格用一个空格代替

《c语言程序设计 第二版》上的题目

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 }

代码二(详细):

 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:偶看了真是泪流满面,泪奔~~o(>_<)o ~~

posted @ 2011-10-28 17:09  fhefh  阅读(3960)  评论(1编辑  收藏  举报