|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include <stdio.h>
int main()
{
char temp[100];
int num[10]={0};
int i,j,blank=0,others=0;
printf("Please input anything what you want:\n");
for(i=0; ; i++)
{
temp[i] = getchar();
if('0' <= temp[i] && temp[i] <= '9')
{
for(j=0; j<10; j++)
{
if(temp[i] == 'j')
{
num[j]++;
}
}
}
else if(temp[i] == '\n' || temp[i] == ' ' || temp[i] == '\t')
{
blank++;
}
else
{
others++;
}
if(temp[i] == '\n')
{
break;
}
}
for(j=0; j<10; j++)
{
printf("%d有%d个\n",j,num[j]);
}
printf("blank have %d\nothers have %d\n",blank,others);
return (0);
}
因为你的if(temp[i] == 'j')判断字符是'j'这个字符,才把计数加1,而前面的if又已经把temp[i]锁定在0到9的字符里面,所以这个if永远不成立
应该改成if (temp[i] == '0'+j)
|
|