-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblemnumber15.cpp
More file actions
35 lines (32 loc) · 830 Bytes
/
Copy pathproblemnumber15.cpp
File metadata and controls
35 lines (32 loc) · 830 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
#include<iostream>
#include<algorithm>
#include<vector>
#include<array>
#include <string>
using namespace std;
//function that calculates the sum of the integers inside a string
int sumOfIntegers(string str) {
int result = 0;
int resulttimer = 0;
for (char s : str)
{
if (isdigit(s))
{
resulttimer = resulttimer * 10 + (s - '0');
}
else
{
result += resulttimer;
resulttimer = 0;
}
}
result += resulttimer;
return result;
}
int main() {
cout << sumOfIntegers("The Great Depression lasted from 1929 to 1939.");
return 0;
}
/*Your task in this kata is to implement a function that calculates the sum of the integers inside a string. For example, in the string "The30quick20brown10f0x1203jumps914ov3r1349the102l4zy dog", the sum of the integers is 3635.
Note: only positive integers will be tested.
*/