Welcome

首页 / 软件开发 / C语言 / 输入一行字符并统计出其中英文字母、空格、数字和其它字符的个数

输入一行字符并统计出其中英文字母、空格、数字和其它字符的个数2011-10-17 本站 smiling cat

题目:

输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。

1.程序分析:利用while语句,条件为输入的字符不为" ".

2.程序源代码:

#include "stdio.h"
#include "conio.h"
main()
{
char c;
int letters=0,space=0,digit=0,others=0;
printf("please input some characters ");
while((c=getchar())!=" ")
{
if(c>="a"&&c<="z"||c>="A"&&c<="Z")
letters++;
else if(c==" ")
space++;
else if(c>="0"&&c<="9")
digit++;
else
others++;
}
printf("all in all:char=%d space=%d digit=%d others=%d ",letters,
space,digit,others);
getch();
}
3.Visual C++ 6.0下调试通过,如图: