- #include<stdio.h>
- #include<ctype.h>
- #define STOP '|'
- int main(void){
- char c;
- char pre;
- int n_all = 0;
- int n_words = 0;
- int n_lines = 0;
- int n_nlines = 0;
- bool inword = false;//判断字符是否在一个单词里面
- printf("Enter a dialog '|' to stop\n");
- while((c = getchar()) != STOP){
- n_all++;
- if(c == '\n'){
- n_lines++;
- }
- if(!isspace(c) && !inword){ //如果一个字符不是空白,而且不在一个单词里面,那么就说明要开启一个新单词了!
- n_words++;
- inword = true;
- }
- if(isspace(c) && inword){ //如果一个字符是空白,而且在一个单词里面,那么这个单词就要结束了!
- inword = false;
- }
- pre = c;//用来记录前一个字符
- }
- if(pre != '\n'){ //如果前一个字符不是“回车”,那么说明存在一个不完整行!
- n_nlines++;
- }
- printf("n_all is %d\n",n_all);
- printf("n_words is %d\n",n_words);
- printf("n_lines is %d\n",n_lines);
- printf("n_nlines is %d\n",n_nlines);
- return 0;
- }