一个关于字符数统计的小程序,该怎么处理
一个关于字符数统计的小程序
用来统计各个数字及空白符以及其他字符出现次数的程序,请教错在那里了?
#include<stdio.h>
void main()
{
int i,c,nwhite,nother;
int ndigit[10];
nwhite=nother=0;
for(i=0;i<10;i++)
ndigit[i]=0;
while((c=getchar())!=EOF)
{
if(c>='0'&&c<=9)
++ndigit[c-0];
else if(c=' '||c='\n'||c='\t')
++nwhite;
else
++nother;
}
printf("digit=");
for(i=0;i<10;i++)
{
printf("%d",i);
printf("\t%d\n",ndigit[i]);
}
printf("nwhite=%d\nnother=%d\n",nwhite,nother);
}
}
------解决方案--------------------
------解决方案--------------------
1 if(c >='0'&& c <= '9') // 应该是字符'9'
2 else if(c == ' '
------解决方案--------------------
c == '\n'
------解决方案--------------------
c == '\t') // 应该是 ==
2 ++ndigit[c-'0']; // 应该是减去字符'0',而不是值0
用来统计各个数字及空白符以及其他字符出现次数的程序,请教错在那里了?
#include<stdio.h>
void main()
{
int i,c,nwhite,nother;
int ndigit[10];
nwhite=nother=0;
for(i=0;i<10;i++)
ndigit[i]=0;
while((c=getchar())!=EOF)
{
if(c>='0'&&c<=9)
++ndigit[c-0];
else if(c=' '||c='\n'||c='\t')
++nwhite;
else
++nother;
}
printf("digit=");
for(i=0;i<10;i++)
{
printf("%d",i);
printf("\t%d\n",ndigit[i]);
}
printf("nwhite=%d\nnother=%d\n",nwhite,nother);
}
}
C
------解决方案--------------------
#include<stdio.h>
void main()
{
int i,c,nwhite,nother;
int ndigit[10];
nwhite=nother=0;
for(i=0;i<10;i++)
{//少一个花括号
ndigit[i]=0;
while((c=getchar())!=EOF)
{
if(c>='0'&&c<=9)
++ndigit[c-0];
else if(c==' '
------解决方案--------------------
c=='\n'
------解决方案--------------------
c=='\t')//判断是双等号
++nwhite;
else
++nother;
}
printf("digit=");
for(i=0;i<10;i++)
{
printf("%d",i);
printf("\t%d\n",ndigit[i]);
}
printf("nwhite=%d\nnother=%d\n",nwhite,nother);
}
}
------解决方案--------------------
1 if(c >='0'&& c <= '9') // 应该是字符'9'
2 else if(c == ' '
------解决方案--------------------
c == '\n'
------解决方案--------------------
c == '\t') // 应该是 ==
2 ++ndigit[c-'0']; // 应该是减去字符'0',而不是值0