-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSumOfNumbersInString.c
More file actions
53 lines (47 loc) · 997 Bytes
/
SumOfNumbersInString.c
File metadata and controls
53 lines (47 loc) · 997 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// { Driver Code Starts
// C++ program to calculate sum of all numbers present
// in a string containing alphanumeric characters
#include <iostream>
using namespace std;
// } Driver Code Ends
#include <cstring>
class Solution
{
public:
//Function to calculate sum of all numbers present in a string.
int findSum(string str)
{
int k=0,i,j,value,n=strlen(str);
int sum=0;
for(i=0;i<n;i++){
j=i;
while(str[j]>=48 && str[j]<=57 && j<n){
value = str[i]-48;
k=k*10;
k+=value;
j+=1;
}
sum+=k;
k=0;
}
return sum;
}
};
// { Driver Code Starts.
// Driver code
int main()
{
// input alphanumeric string
int t;
cin>>t;
while(t--)
{
string str;
cin>>str;
Solution obj;
cout << obj.findSum(str);
cout<<endl;
}
return 0;
}
// } Driver Code Ends