《C Primer Plus》第十一章编程练习11-12 萌新!

萌新报道!

本人算是个小白,目前学到《C Primer Plus》第十二章。

准备开始发些博客,既是展现自己的渣渣实力,也是结交IT界的大牛们,嘿嘿!

编译环境:VS 2017 Community

运行平台:Win 10 ×64

好了,话不多说,上题目:

/*	11-12 ---编写一个程序,读取输入,直至读到 EOF ,报告读入的单词数,
	大写字母数,小写字母数,标点字符数和数字字符数。
	使用ctype.h头文件中的函数。
*/

从题目中看......有以下几个要求,以及对应的<ctype.h>中的函数:

  • 单词数量                            isspace()函数检测空白字符
  • 大写字母数                         isupper()函数
  • 小写字母数                         islower()函数
  • 标点符号数                         ispunct()函数
  • 数字字符数                         isdigit()函数

既然思路理清了,就直接上代码了(此萌新尤其注意代码格式)

#include <stdio.h>
#include <ctype.h>					//处理字符输入

#define SIZE 256					//字符数组最大长度

int main(void)
{
	char Input[SIZE];				//储存输入的字符
	char ch;				        //检查当前输入的字符
	
	int words = 0;				        //储存单词数
	int upletters = 0;				//储存大写字母数
	int lowletters = 0;				//储存小写字母数
	int puncts = 0;					//储存标点符号数目
	int digits = 0;					//储存数字字母数目
	int count = 0;					//循环变量

	printf("输入一些字符,输入[Ctrl] + Z 结束输入:\n\n");//提示输入

	/* 因为需要处理每个字符的,所以使用getchar() */
	while ((ch = getchar()) != EOF)
	{
		if (isupper(ch))			//大写字母(返回 1 )
		{
			upletters++;
		}
		else if (islower(ch))			//小写字母
		{
			lowletters++;
		}
		else if (ispunct(ch))			//标点符号
		{
			puncts++;
		}
		else if (isdigit(ch))			//数字字符
		{
			digits++;
		}
		else if (isspace(ch) &&
			isspace(Input[count - 1]) == 0)	//检测单词(检测到空格时,
		{											                                        //如果前一个字符不是空格,
			words++;			//则判断为单词)
		}	
		Input[count] = ch;
		count++;
	}
	printf("\n\n好的,在你的输入中有:\n");
	printf("%-5d 个单词\n", words);
	printf("%-5d 个大写字母\n", upletters);
	printf("%-5d 个小写字母\n", lowletters);
	printf("%-5d 个标点符号\n", puncts);
	printf("%-5d 个数字字符\n", digits);
	printf("\n\n感谢你的使用呦!\n");

	return 0;
}

好的,代码以上。现在我们来看看运行效果:

OK,就到这里了!

感谢大佬们观摩!!!

告辞!

猜你喜欢

转载自blog.csdn.net/Alex_mercer_boy/article/details/81452638