#include<stdio.h>#include<stdbool.h>#include<ctype.h>#define STOP '|'int main(void){ char c; // 读入字符 char prev; //读入的前一个字符 long n_chars = 0L;//字符数 int n_lines = 0;//行数 int n_words = 0;//单词数 int p_lines = 0;//不完整的行数 bool inword = false;//如果C在单词中,inword 等于true printf("Enter text to be analyzed (| to terminate):"); prev = '\n'; //用于识别完整的行 while((c = getchar()) != STOP) { n_chars++; //统计字符 if(c == '\n') n_lines++; //统计行 if(!isspace(c) && !inword) { inword = true;//开始一个新的单词 n_words++;//统计单词 } if(isspace(c) && inword) inword = false;//达到单词的末尾 prev = c; } if(prev != '\n') p_lines = 1; printf("characters = %ld, words = %d, limes = %d,",n_chars,n_words,n_lines); printf("partial lines = %d\n",p_lines); return 0;}输入: Reason is apowerful servant butan inadequate master.|问:从while开始,这个循环是 从输入的内容中 一个字符一个字符的循环一遍?还是直接整体开始循环的?
1 回答
- 1 回答
- 0 关注
- 1373 浏览
添加回答
举报
0/150
提交
取消