NOIP学习之字符串:58.统计数字字符个数

测试链接
总时间限制: 1000ms 内存限制: 65536kB
描述
输入一行字符,统计出其中数字字符的个数。

输入
一行字符串,总长度不超过255。
输出
输出为1行,输出字符串里面数字字符的个数。
样例输入
Peking University is set up at 1898.
样例输出
4

#include <iostream>
#include<cstring>
char ch[256];
using namespace std;

int main()
{
    int n,len;
    int sum=0;
    
	gets(ch);//获取一行字符串
    len=strlen(ch);//求字符串长度
    
    for(int i=0;i<=len;i++)//对字符串进行遍历
    	if(ch[i]>='0'&&ch[i]<='9')//如果是数字
            sum++;//计数器+1
    
    cout<<sum<<endl;//输入统计的个数
     
    return 0;

}
发布了80 篇原创文章 · 获赞 0 · 访问量 778

猜你喜欢

转载自blog.csdn.net/wlxiong/article/details/104441383
今日推荐