C 统计单词个数

 1 #includestdio.h
 2 
 3 #define IN  1/*在单词内*/
 4 #define OUT 0/*在单词外*/
 5 int main(int argc, char* argv[])
 6 {
 7     int c, nl, nw, nc, state;
 8     state = OUT;
 9     nl = nw = nc = 0;
10     while((c = getchar())!=EOF)
11     {
12         nc++;
13         if(c=='\n')
14         {
15             nl++;
16         }
17         if(c==' ' || c== '\n' || c== '\t')
18         {
19             state = OUT;
20         }
21         else if(state == OUT)
22         {
23             state = IN;
24             nw++;
25         }
26     }
27     printf("%d %d %d\n",nl,nw,nc);
28     return 0;
29 }

 

 1 /*编写一个程序,统计字符数、单词数、行数*/
 2 
 3 #include <stdio.h>
 4 
 5 int main()
 6 {
 7     int c;
 8     int nc = 0;
 9     int nw = 0;
10     int nl = 0;
11     int wordin = 0;
12     while( (c = getchar())!=EOF ){
13         ++nc;
14         if(c == '\n'){
15             ++nl;
16         }
17         if( (c!='\t') && ( c != '\n' ) && ( c != ' ') ){
18             wordin =1;
19         }
20         else{
21             if( wordin == 1){
22                 nw++;
23                 wordin = 0;
24             }
25         }
26     }
27     printf("c\tl\tw\n");
28     printf("%d\t%d\t%d\n",nc,nl,nw);
29     return 0;
30 }

 

posted @ 2014-04-22 15:42  永久指针  阅读(632)  评论(0编辑  收藏  举报