码迷,mamicode.com
首页 > 其他好文 > 详细

The C Programming Language Exercise

时间:2017-01-04 00:18:42      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:one   color   sig   poi   ram   lang   single   des   design   

1-9 :

Write a program to copy its input to its output, replacing each string of one or more blanks by a single blank. 

point : change the entry of space putchar when there has met a space already.

#include <stdio.h>
main(){
    int c = 0;
    int inspace = 0;
    while((c = getchar()) != EOF){
        if(c ==  ){
            if(inspace == 0){
                putchar(c);
                inspace = 1;
            }
        }
        else{
            inspace = 0;
            putchar(c);
        }
    }
}

 count the numbers of word:

 1 #include <stdio.h>
 2 #define IN 1  //no ";"
 3 #define OUT 0
 4 
 5 main(){
 6     int c ;
 7     int state = OUT;
 8     int nc, nw, nl;
 9     nc = nw = nl = 0;  //注意初始化 
10     
11     while((c = getchar()) != EOF){
12         nc ++; 
13         if(c == \t)
14             nl++;
15         if(c ==   || c == \t || c == \n) {
16             state = OUT;             
17         }    
18         else if(state == OUT){  //if state is OUT here, means in the word, chage state and ++nw
19             state = IN;
20             nw++;
21         }
22     }
23     printf("%d %d %d",nc, nw, nl);
24 }

 design a program output the result that every line contains only a word

 1 #include <stdio.h>
 2 #define IN 1  //no ";"
 3 #define OUT 0
 4 
 5 main(){
 6     int c ;
 7     int inspace = 0;
 8     while((c = getchar()) != EOF){
 9         if(c ==   || c == \t || c == \t){
10             if(inspace == 0){
11                 inspace = 1;
12                 putchar(\n);
13             }
14         }
15         else{
16             inspace = 0;
17             putchar(c);
18         }
19     }
20     return 0;
21 }

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

The C Programming Language Exercise

标签:one   color   sig   poi   ram   lang   single   des   design   

原文地址:http://www.cnblogs.com/crazyRanZhang/p/6246834.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!