1160: 零起點學算法67——統計字母數字等個數


1160: 零起點學算法67——統計字母數字等個數

Time Limit: 1 Sec  Memory Limit: 64 MB   64bit IO Format: %lld
Submitted: 2697  Accepted: 1339
[Submit][Status][Web Board]

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]


 

 

Source

 
 
 1 #include<stdio.h>
 2 #include<string.h>
 3 int main(){
 4     char ch[100];
 5     while(gets(ch)!=NULL){
 6         int a=0,b=0,c=0,d=0;
 7         for(int i=0;ch[i]!='\0';i++){
 8             if((ch[i]>='a'&&ch[i]<='z')||(ch[i]>='A'&&ch[i]<='Z')){
 9                 a++;
10             }
11             else if(ch[i]>='0'&&ch[i]<='9'){
12                 b++;
13             }
14             else if(ch[i]==' '){
15                 c++;
16             }
17             else{
18                 d++;
19             }
20         }
21         printf("%d %d %d %d\n",a,b,c,d);
22     }
23     return 0;
24 } 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM