zoukankan      html  css  js  c++  java
  • C程序设计语言习题(112)

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

     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 }
  • 相关阅读:
    unittest learning
    C++类和对象
    Linux shell基础(十二)
    Linux shell基础(十一)
    Linux shell基础(十)
    Linux shell基础(九)
    Linux shell基础(八)
    Linux shell基础(六)
    Linux shell基础(七)
    Linux shell基础(五)
  • 原文地址:https://www.cnblogs.com/cpoint/p/3367500.html
Copyright © 2011-2022 走看看