char_cat.c

来自「c和指针 学习c语言必须阅读的书籍之一 提高对C语言的掌握理解能力」· C语言 代码 · 共 94 行

C
94
字号
/*
** Compute the percentage of characters read from the standard
** input that are in each of several character categories.
*/
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>

/*
**	Define a function to compute whether a character is not
**	printable; this eliminates a special case for this
**	category in the code below.
*/
int is_not_print( int ch )
{
	return !isprint( ch );
}

/*
**	Jump table of classification functions for each category.
*/
static	int	(*test_func[])( int ) = {
	iscntrl,
	isspace,
	isdigit,
	islower,
	isupper,
	ispunct,
	is_not_print
};
#define	N_CATEGORIES	\
	    ( sizeof( test_func ) / sizeof( test_func[ 0 ] ) )

/*
**	The name of each of the character categories.
*/
char	*label[] = {
	"control",
	"whitespace",
	"digit",
	"lower case",
	"upper case",
	"punctuation",
	"non-printable"
};

/*
**	Number of characters seen in each category so far, and
**	total # of characters.
*/
int	count[ N_CATEGORIES ];
int	total;

main()
{
	int	ch;
	int	category;

	/*
	** Read and process each character
	*/
	while( (ch = getchar()) != EOF ){
		total += 1;

		/*
		** Call each of the test functions with this
		** character; if true, increment the associated
		** counter.
		*/
		for( category = 0; category < N_CATEGORIES;
		    category += 1 ){
			if( test_func[ category ]( ch ) )
				count[ category ] += 1;
		}
	}

	/*
	** Print the results.
	*/
	if( total == 0 ){
		printf( "No characters in the input!\n" );
	}
	else {
		for( category = 0; category < N_CATEGORIES;
		    category += 1 ){
			printf( "%3.0f%% %s characters\n",
			    count[ category ] * 100.0 / total,
			    label[ category ] );
		}
	}

	return EXIT_SUCCESS;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?