pta统计字符串中数字字符的个数
输入一个字符串,统计其中数字字符(‘0’……‘9’)的个数。
输入格式:
输入在一行中给出一个不超过80个字符长度的、以回车结束的非空字符串。
输出格式:
输出所统计的数字字符的个数。
输入样例:
Enter a string: It’s 512?
结尾无空行
输出样例:
3
结尾无空行
#include <stdio.h>
int main()
{
char a[80];
int i = 0, count = 0;
while ((a[i] = getchar()) != '\n')
{
i++;
}
a[i] = '\0';
for (i = 0; a[i] != '\0'; i++)
{
if (a[i] <= '9' && a[i] >= '0')
{
count++;
}
}
printf("%d", count);
return 0;
}