C程序设计语言习题(1-12)

统计行数、单词数,字符数的程序: 

 1 #include<stdio.h>
 2 
 3 #define IN    1   /*在单词内*/
 4 #define OUT   0   /*在单词外*/
 5 
 6 int main()
 7 {
 8     int c, nl, nw, nc, state;
 9 
10     state = OUT;
11     nl = nw = nc  = 0;    //nl:行数  nw:单词数  nc:字符数
12     while((c = getchar()) != EOF) {
13         ++nc;
14         if(c == '\n')
15             ++nl;
16         if(c == ' ' || c == '\n' || c == '\t')
17             state = OUT;
18         else if (state == OUT) {
19             state = IN;
20             ++nw;
21         }
22     }
23     printf("%d %d %d\n", nl, nw, nc);
24     return 0;
25 }  

【练习1-12】 
编写一个程序,以每行一个单词的形式打印其输入 

 1 #include<stdio.h>
 2 
 3 #define IN    1   /*在单词内*/
 4 #define OUT   0   /*在单词外*/
 5 
 6 int main()
 7 {
 8     int c, state;
 9     state = OUT;
10     
11     while((c =getchar()) != EOF) {
12         if(c == ' ' || c == '\n' || c == '\t') {
13             if(state == IN) {
14                 putchar('\n');
15                 state = OUT;
16             }
17         } else if(state == OUT) {
18             state = IN;
19             putchar(c);
20         } else {
21             putchar(c);
22         }
23     }
24     return 0;
25 }

 

转载于:https://www.cnblogs.com/cpoint/p/3367500.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值