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

<C Primer Plus>11 A Word-Count Program

时间:2017-04-04 14:56:52      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:init   stdio.h   getchar   blog   div   clu   his   primer   prim   

#include <stdio.h>
#include "ctype.h"
#define STOP  ‘|‘
int main(void){
    char c;//Initializing
    long n_char = 0L;
    int n_word = 0;
    int n_line = 0;
    int inword = 0;//This is a flag making sure in word or not .
    
    while ((c = getchar()) != STOP){//
        n_char++;
        if (c == \n){
            n_line++;
        }
        if (!isspace(c) && !inword){//c = getchar(), isn‘t space and not in word.
            inword = 1;
            n_word++;
        }
        if (isspace(c) && inword){// c is space and in word.
            inword = 0;
        }
    }
    printf("characters = %ld, word = %d, line = %d", n_char, n_word, n_line);

    return 0;
}

 

<C Primer Plus>11 A Word-Count Program

标签:init   stdio.h   getchar   blog   div   clu   his   primer   prim   

原文地址:http://www.cnblogs.com/michael2016/p/6664993.html

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