统计字数的小程序(1)
读取输入的的字符并报告其中的字符、单词以及行数。
c primer plus 7.7
1 #include<stdio.h> 2 #include<stdbool.h> 3 #include<ctype.h> 4 #define STOP \']\' 5 int main(void) 6 { char c; 7 int n_character=0; 8 int n_word= 0; 9 int n_line = 0; 10 int p_line = 0; 11 bool inword = false; 12 char prev ; 13 printf("Please enter the strings to be analysis:\n"); 14 prev= \'\n\'; 15 16 while((c=getchar())!=STOP) 17 { n_character++; 18 if(!inword && !isspace(c)) 19 { inword = true; 20 n_word++; 21 } 22 if(inword && isspace(c)) inword = false; 23 if(c == \'\n\') n_line++; 24 prev = c; 25 } 26 27 if(prev !=\'\n\' ) p_line = 1; 28 29 printf(" charaters : %d\n words: %d\n lines: %d\n partical lines: %d\n", 30 n_character,n_word,n_line,p_line); 31 return 0; 32 }
请发表评论