编程 C语言
输入一行字符分别统计其中英文字母、空格、数字和其他字符的个数。C语言程序,新手请前辈指教!
#include <stdio.h> #include <ctype.h> #include <string.h> #define ARRAYMAX 7 int is_not_print( int ch ); int ( *TestFunc[ ARRAYMAX ] )( int ch ) = { iscntrl, isspace, isdigit, islower, isupper, ispunct, is_not_print }; int Test[ ARRAYMAX ]; char *TypeName[ ARRAYMAX ] = { "iscntrl", "isspace", "isdigit", "islower", "isupper", "ispunct", "is_not_print" }; int main( void ) { int ch; int count; int i; count = 0; while( EOF != ( ch = getchar() ) ) { count++; for( i = 0; i < ARRAYMAX; i++ ) if( TestFunc[ i ]( ch ) ) Test[ i ]++; } for( i = 0; i < ARRAYMAX; i++ ) { printf( "%-*s : %.2f%%\n", strlen( "is_not_print" ), TypeName[ i ], ( float )( Test[ i ] * 100 / count ) ); } return 0; } int is_not_print( int ch ) { return !isprint( ch ); }