标签:blog print col white not count get etc std
练习 1-13 编写一个程序,打印输入中单词长度的直方图。
1 #include <stdio.h> 2 3 /* count digits, white space, others */ 4 5 int main(int argc, char const *argv[]) 6 { 7 int c, i, j, nwhite, nother; 8 int ndigit[10]; 9 10 nwhite = nother = 0; 11 for (i = 0; i < 10; ++i) { 12 ndigit[i] = 0; 13 } 14 15 while ((c = getchar()) != EOF) { 16 if (c >= ‘0‘ && c <= ‘9‘) { 17 ++ndigit[c-‘0‘]; 18 } else if (c == ‘ ‘ || c == ‘\n‘ || c == ‘\t‘) { 19 ++nwhite; 20 } else { 21 ++nother; 22 } 23 } 24 25 // printf("digits ="); 26 for (i = 0; i < 10; ++i) { 27 printf("%d:", i); 28 for (j = 0; j < ndigit[i]; ++j) { 29 printf("*"); 30 } 31 printf("\n"); 32 } 33 printf("w:"); 34 for (i = 0; i < nwhite; ++i) { 35 printf("*"); 36 } 37 printf("\no:"); 38 for (i = 0; i < nother; ++i) { 39 printf("*"); 40 } 41 printf("\n"); 42 // printf(", white space = %d, others = %d\n", nwhite, nother); 43 44 return 0; 45 }
标签:blog print col white not count get etc std
原文地址:http://www.cnblogs.com/berthua/p/7694789.html