oj網站的訓練題:統計字符

題目描述:

輸入一行字符,分別統計出其中英文字母、空格、數字和其它字符的個數。


樣例輸入:

a 1,


樣例輸出:


1
1
1
1


參考代碼:


#include<stdio.h>  
#include<string.h>    
int main()  
{  
    int letter=0,space=0,number=0,other=0;  
    int i;  
    char s[100];  
    gets(s);  
    for(i=0;i<strlen(s);i++)  
    {  
        if(s[i]>='A'&&s[i]<='Z'||s[i]>='a'&&s[i]<='z')  
            letter++;  
        else if(s[i]==' ')  
            space++;  
        else if(s[i]<='9'&&s[i]>='0')  
            number++;  
        else  
            other++;  
    }  
    printf("%d\n%d\n%d\n%d\n",letter,space,number,other);  
} 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章