統計字母數字等個數
阿新 • • 發佈:2018-12-02
Description
輸入一串字元,統計這串字元裡的字母個數,數字個數,空格字數以及其他字元(最多不超過100個字元)
Input
多組測試資料,每行一組
Output
每組輸出一行,分別是字母個數,數字個數,空格字數以及其他字元個數
Sample Input
I am a student in class 1.
I think I can!
Sample Output
18 1 6 1
10 0 3 1
HINT
char str[100];//定義字元型陣列
while(gets(str)!=NULL)//多組資料
{
//輸入程式碼
for(i=0;str[i]!=’\0’;i++)//gets函式自動在str後面新增’\0’作為結束標誌
{
//輸入程式碼
}
//字元常量的表示,
'a’表示字元a;
'0’表示字元0;
//字元的賦值
str[i]=‘a’;//表示將字元a賦值給str[i]
str[i]=‘0’;//表示將字元0賦值給str[i]
}
#include <stdio.h>
int main() {
char str[101];//定義字元型陣列//可以定義大一點
while(gets(str)!=NULL)//多組資料
{
//輸入程式碼
int i;
int a[4];//可以用bcde代替
a[0]=0;//字母
a[1]=0;//數字
a[2]=0;//空格
a[3]=0;//其他
for(i=0;str[i]!=’\0’;i++)//gets函式自動在str後面新增’\0’作為結束標誌
{
if(str[i]>=‘a’&&str[i]<=‘z’||str[i]>=‘A’&&str[i]<=‘Z’){
a[0]+=1;
}
else if(str[i]>='0'&&str[i]<='9'){
a[1]+=1;
}
else if(str[i]==' '){
a[2]+=1;
}
else{
a[3]+=1;
}
}
printf("%d %d %d %d\n",a[0],a[1],a[2],a[3]);
}
return 0;
}