Description
输入一行字符,分别统计其中英文字母、空格、数字和其它字符的个数。
Input
键盘上的任意字符串,以;结束。
Output
character= ,blank= ,digit= ,other= ;
Sample Input
case 1:printf("very good!");
Sample Output
character=18,blank=2,digit=1,other=6;
Description
输入一行字符,分别统计其中英文字母、空格、数字和其它字符的个数。
Input
键盘上的任意字符串,以;结束。
Output
character= ,blank= ,digit= ,other= ;
Sample Input
case 1:printf("very good!");
Sample Output
character=18,blank=2,digit=1,other=6;
我这个答案是正确的 但运行超时 我不知道如何简化
#include <stdio.h>
int main()
{ char str[100];
int character=0,blank=0,digit=0,other=0,i=0;
printf("",str);
while((str[i]=getchar())!='\n')
{
if (str[i]>='a'&&str[i]<='z'||str[i] >='A'&&str[i]<='Z')
{
character++;
}
else if(str[i]==' ')
{
blank++;
}
else if (str[i]>='0'&&str[i]<='9')
{
digit++;
}
else
{
other++;
}
}
printf("character=%d,blank=%d,digit=%d,other=%d;",character,blank,digit,other);
return 0;
}