7-90 統計字符 (15分)
本題要求編寫程序,輸入10個字符,統計其中英文字母、空格或回車、數字字符和其他字符的個數。
輸入格式:
輸入為10個字符。最后一個回車表示輸入結束,不算在內。
輸出格式:
在一行內按照
letter = 英文字母個數, blank = 空格或回車個數, digit = 數字字符個數, other = 其他字符個數
的格式輸出。
輸入樣例:
aZ &
09 Az
輸出樣例:
letter = 4, blank = 3, digit = 2, other = 1
#include<stdio.h>
#include<string.h>
int main()
{
char c;
int count=0;
int letter=0,space=0,digit=0,other=0;
while(count++<=9)
{
c=getchar();
if( ' '== c|| '\n' == c)
{
space++;
}
else
if(c>='a'&&c<='z'||c>='A'&&c<='Z')
letter++;
else
if(c>='0'&&c<='9')
digit++;
else
other++;
}
printf("letter = %d, blank = %d, digit = %d, other = %d\n",letter,space,digit,other);
#include<string.h>
int main()
{
char c;
int count=0;
int letter=0,space=0,digit=0,other=0;
while(count++<=9)
{
c=getchar();
if( ' '== c|| '\n' == c)
{
space++;
}
else
if(c>='a'&&c<='z'||c>='A'&&c<='Z')
letter++;
else
if(c>='0'&&c<='9')
digit++;
else
other++;
}
printf("letter = %d, blank = %d, digit = %d, other = %d\n",letter,space,digit,other);
return 0;
}
}