#K38767. Sum of Numbers in a String
Sum of Numbers in a String
Sum of Numbers in a String
You are given a single line string which may contain integers and non-numeric tokens separated by spaces. Your task is to compute the sum of all valid integers present in the string. A token is considered a valid integer if it consists only of an optional sign (either '+' or '-') followed by digits. Any token that does not strictly represent an integer should be ignored. If no valid integer is found, the result is 0.
Note: Splitting of the string is done using any whitespace character. For example, multiple consecutive spaces should be treated as a single delimiter.
The mathematical formulation of the problem can be represented as follows:
[ \text{Result} = \sum_{i=1}^{n} a_i \quad \text{where } a_i \text{ is the } i\text{-th valid integer in the input string.} ]
inputFormat
Input is provided via standard input as a single line that contains a string. The string may contain multiple tokens separated by whitespace.
outputFormat
Output the sum of all valid numbers found in the string to standard output.## sample
12 4 85 3 0
104