基于visual Studio2013解决C语言竞赛题之0403字符统计





题目


解决代码及点评

这道题的目标是锻炼while循环,while循环的语法是

while(循环条件)

{

    循环体;

}

每次进入循环体之前,先判断循环条件是否满足,如果不满足,执行序列就跳过

循环体


//3.	输入一行字符,统计其中的英文字母、数字、空格和其他字符个数。
#include <stdio.h>
#include <stdlib.h>
void	main()
{
	char x;
	int numchar=0;
	int numnum=0;
	int numspace=0;
	int numother=0;

	printf("please input string\n");
	while((x=getchar())!='\n')    // getchar()获得用户输入的一个字符,循环的获取,如果获取的字符不是'\n',就处理,否则退出
	{
		if(x>='A'&&x<='Z'||x>='a'&&x<='z')   // 按照要求进行统计,如果是字母,则字母累加1
		{
			numchar++;
		}
		else if (x>='0'&&x<='9')  // 如果是数字,则数字累加1
		{
			numnum++;
		}
		else if (x==' ') // 如果是空格,则空格累加1
		{
			numspace++;
		}
		else
			numother++;   // 否则,其他累加1
	}
// 打印结果
	printf("英文字符个数为%d\n",numchar);
	printf("数字字符个数为%d\n",numnum);
	printf("空格字符个数为%d\n",numspace);
	printf("其他字符个数为%d\n",numother);



	system("pause");
}


代码下载及其运行

代码下载链接:

http://download.csdn.net/detail/yincheng01/6640731

解压密码为c.itcast.cn


下载解压后用VS2013打开工程文件

点击 “本地Windows调试器” 执行


程序运行结果









posted on 2013-12-02 22:44  三少爷的剑123  阅读(142)  评论(0编辑  收藏  举报

导航