#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 回答
- 0 關注
- 1399 瀏覽
添加回答
舉報
0/150
提交
取消