HDU2017字符串统计

问题遇到的现象和发生背景 对于给定的一个字符串,统计其中数字字符出现的次数。输入数据有多行,第一行是一个整数n,表示测试实例的个数,后面跟着n行,每行包括一个由字母和数字组成的字符串。对于每个测试实例,输出该串中数值的个数,每个输出占一行。

Input
2
fasdf123123asdsdf
f111111111asdfasf
Output
6
9

运行结果及报错内容
我的解答思路和尝试过的方法 如何实现在不确定字符串长度的情况下检测是否到字符串尾

scanf输入字符串,逐个字符遍历,判断是否在'0'到'9'之间

#include <stdio.h>
int main()
{
    int n,i,j=0,count=0;
    char s[1000] = {0};
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        scanf("%s",s);
        j=0;
        count=0;
        while(s[j] != '\0')
        {
            if(s[j] >= '0' && s[j] <='9')
                 count++;
            j++;
        }
        printf("%d\n",count);
    }
    return 0;
}

供参考:

#include <stdio.h>
#include <ctype.h>
int main()
{
    int T, i, cnt;
    char s[128];
    scanf("%d", &T);
    while (T--) {
        cnt = 0; i = 0;
        scanf("%s", s);
        while (s[i]) 
            isdigit(s[i++]) ? cnt++ : NULL;
        printf("%d\n", cnt);
    }
    return 0;
}