首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

要统计字母、数字、空格,和字符的个数,错在哪?解决思路

2012-03-14 
要统计字母、数字、空格,和字符的个数,错在哪?#includestdio.hvoidmain(){intEnglish,space,Num,othercha

要统计字母、数字、空格,和字符的个数,错在哪?
#include   <stdio.h>
void   main()
{
int   English,space,Num,other;
char   inp,ans;
ans= 'y '   ;
do
{
printf( "\n请输入字符串:\n ");
English=0;
space=0;
Num=0;
other=0;
fflush(stdin);
do   {
inp=getchar();
if(inp> = '0 '   &&   inp <= '9 ')
Num++;
else   if(inp= '   ')
space++;
else   if(inp> = 'a '   &&   inp <= 'z ')
English++;
else   if(inp> = 'A '   &&   inp <= 'Z ')
English++;
else
other++;
}while(inp!= '\n ');
printf( "\n输入的字符串中英文为:%d个 ",--English);
printf( "\n输入的字符串中空格为:%d个 ",--space);
printf( "\n输入的字符串中数字为:%d个 ",--Num);
printf( "\n输入的字符串中其他为:%d个 ",--other);
printf( "\n是否需要输入其他的字符串?(Y/N) ");
ans=getchar();
}while   (ans== 'y '||ans== 'Y ');
}


[解决办法]
#include <stdio.h>

int _tmain(int argc, _TCHAR* argv[])
{
int English,space,Num,other;
char inp,ans;
ans= 'y ' ;
do{
printf( "\n请输入字符串:\n ");
English=0;
space=0;
Num=0;
other=0;
fflush(stdin);
do {
inp=getchar();
if(inp> = '0 ' && inp <= '9 ')
Num++;
else if(inp== ' ')
space++;
else if(inp> = 'a ' && inp <= 'z ')
English++;
else if(inp> = 'A ' && inp <= 'Z ')
English++;
else
other++;
}while(inp!=10);
printf( "\n输入的字符串中英文为:%d个 ",English);
printf( "\n输入的字符串中空格为:%d个 ",space);
printf( "\n输入的字符串中数字为:%d个 ",Num);
printf( "\n输入的字符串中其他为:%d个 ",other);
printf( "\n是否需要输入其他的字符串?(Y/N) ");
ans=getchar();
}while (ans== 'y '||ans== 'Y ');
return 0;
}

[解决办法]
a、}while(inp!=10);

10就是\n的ascii码,和使用 '\n '一样的,没什么关系

b、return 0;

因为main的定义是int的,所以需要return
当然void也可以,但不符合规矩(强烈建议按c99的规范来做)

热点排行