题目描述

请编写一个程序实现以下功能:从一个字符串中,提取出所有的数字字符即0-9,并作为数求和。

输入

一行字符串,长度不超过100,字符串中不含空格。

输出

字符串中所有数字字符作为数的和

样例输入 复制
Lsd2f02k3ja3sdf223
样例输出 复制
17
代码 
#include<bits/stdc++.h>
using namespace std;
int main(){
	string a;int s=0;
	cin>>a;
	int z=a.length();
	for(int i=0;i<z;i++){
		if(a[i]>='0'&&a[i]<='9'){
			s=s+(a[i]-48);
		}
	}
	cout<<s;
	return 0;
}

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐