编写程序,对于给定的一个字符串,统计其中数字字符出现的次数。
#include <stdio.h>
int main()
{
char str[] = "Hello, 123 world!";
int count = 0;
for (int i = 0; str[i]!= '\0'; i++)
{
if (isdigit(str[i]))
{
count++;
}
}
printf("数字字符出现的次数为:%d\n", count);
return 0;
}
注意isdigit 函数是用来计算数字的个数的
这里用的是一层for循环来录入初定义数组中的数据
嵌套一个 if 语句,用来筛选符合条件的元素
每识别到一个符合条件的元素
就让用来计数的 count 变量加一
最后输出 count 的值,即为题目所求