输入字符统计个数数字部分始终计不了数
#include <stdio.h>int main()
{
char temp;
int num={0};
int i,j,blank=0,others=0;
printf("Please input anything what you want:\n");
for(i=0; ; i++)
{
temp = getchar();
if('0' <= temp && temp <= '9')
{
for(j=0; j<10; j++)
{
if(temp == 'j')
{
num++;
}
}
}
else if(temp == '\n' || temp == ' ' || temp == '\t')
{
blank++;
}
else
{
others++;
}
if(temp == '\n')
{
break;
}
}
for(j=0; j<10; j++)
{
printf("%d有%d个\n",j,num);
}
printf("blank have %d\nothers have %d\n",blank,others);
return (0);
}
#include<stdio.h>
#include<string.h>
#define N 100
char arr = {0};
int main()
{
printf("Please input something:\n");
scanf("%[^\n]&*c", arr);
int len = strlen(arr);
int i;
int sum = 0;
for(i=0;i<len;i++)
{
if(arr != ' ')
{
sum++;
}
}
printf("The total num of character is %d.\n", sum);
return 0;
} 因为你的if(temp == 'j')判断字符是'j'这个字符,才把计数加1,而前面的if又已经把temp锁定在0到9的字符里面,所以这个if永远不成立
应该改成if (temp == '0'+j)
页:
[1]