#C1358. Sum of Numbers in a String
Sum of Numbers in a String
Sum of Numbers in a String
You are given a string s
which contains a mix of alphabetic characters and digits. Your task is to extract all contiguous substrings that represent non-negative integers, convert them to numbers, and then compute the sum of these numbers.
For example, given the string abc123xyz45
, the numerical substrings are 123
and 45
. The answer is:
\(123 + 45 = 168\)
More formally, if the string contains numerical substrings \(n_1, n_2, \dots, n_k\), you should compute the sum:
\(\sum_{i=1}^{k}{n_i}\)
If the string does not contain any digits, the sum is 0.
inputFormat
The input consists of a single line containing the string s
which may contain both letters and digits.
outputFormat
Output a single integer representing the sum of all numerical substrings found in s
.
abc123xyz45
168