Add Numbers Inside a String
Problem Description​
Given a string that contains both letters and numbers, write a function that pulls out all the numbers and returns their sum. The numbers are contiguous sequences of digits within the string.
Input​
- A single string that contains both letters and numbers.
Output​
- An integer that is the sum of all the numbers in the string.
Constraints​
- The input string will contain only alphanumeric characters.
Example​
Input:
"a1b2c3"Output:
6Explanation:
The numbers in the string are 1, 2, and 3. Their sum is 6.
Input:
"a10b20c30"Output:
60Explanation:
The numbers in the string are 10, 20, and 30. Their sum is 60.
Input:
"h8ers"Output:
8Explanation:
The number in the string is 8. There are no other numbers, so the sum is 8.