1. 程式人生 > >HDOJ字串統計

HDOJ字串統計

Problem Description 對於給定的一個字串,統計其中數字字元出現的次數。  

 

Input 輸入資料有多行,第一行是一個整數n,表示測試例項的個數,後面跟著n行,每行包括一個由字母和數字組成的字串。  

 

Output 對於每個測試例項,輸出該串中數值的個數,每個輸出佔一行。  

 

Sample Input 2 asdfasdf123123asdfasdf asdf111111111asdfasdfasdf  

 

Sample Output 6 9  

 

Author lcy  
#include <stdio.h>
#include <string.h>
int main()
{
    int n, i, len, j, sum;
    char  a[100];
    while(scanf("%d", &n)!=EOF)
    {
        getchar();
        for(i=1; i<=n; i++)
        {
            gets(a);
            len=strlen(a);
            sum
=0; for(j=0; j<len; j++) { if(a[j]>='0' && a[j]<='9') { sum++; } } printf("%d\n", sum); } } return 0; }