现有一道C语言题想请语言高手们求解,请你帮帮忙

现有一道C语言题想请语言高手们帮忙求解,请你帮帮忙,
(用户从键盘输入一行字符,分别统计数其中的英文字母,空格,数字,占字符总数的百分比)。

#include <stdio.h>
#include <ctype.h>

int main()
{
int digitnum = 0;
int alphanum = 0;
int spacenum = 0;
double totalnum = 0;
while(gc = getchar() != EOF)
{
if(isdigit(gc)) digitnum++;
else if(gc == ' ') spacenum++;
else if(isalpha(gc)) alphanum++;
totalnum++;
}
totalnum *= 0.01;
printf("Space: %lf%%, Digit: %lf%%, Alphabet: %lf%%", spacenum/totalnum, digitnum/totalnum, alphanum/totalnum);
return 0;
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2006-12-28
你是初学者吗?先看这道吧
题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
1.程序分析:利用while语句,条件为输入的字符不为'\n'.

2.程序源代码:
#include "stdio.h"
main()
{char c;
int letters=0,space=0,digit=0,others=0;
printf("please input some characters\n");
while((c=getchar())!='\n')
{
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\n",letters,
space,digit,others);
}
这些都可以在网上查的,好好学!
相似回答
大家正在搜