the.c

来自「与C语言四书五经之《C和指针》的配套的书上源代码。」· C语言 代码 · 共 43 行

C
43
字号
/*
** Count the number of times the word "the" appears in the standard
** input.  Case is important, and the words in the input are
** separated from each other by one or more white space characters.
*/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

char	const	whitespace[] = " \n\r\f\t\v";

int
main()
{
	char	buffer[101];
	int	count;

	count = 0;

	/*
	** Read lines until EOF is found.
	*/
	while( gets( buffer ) ){
		char	*word;

		/*
		** Extract words from the buffer one by one
		** until there are no more.
		*/
		for( word = strtok( buffer, whitespace );
		    word != NULL;
		    word = strtok( NULL, whitespace ) ){
			if( strcmp( word, "the" ) == 0 )
				count += 1;
		}
	}

	printf( "%d\n", count );

	return EXIT_SUCCESS;
}

⌨️ 快捷键说明

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