Skip to main content

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:
6

Explanation:
The numbers in the string are 1, 2, and 3. Their sum is 6.

Input:
"a10b20c30"

Output:
60

Explanation:
The numbers in the string are 10, 20, and 30. Their sum is 60.

Input:
"h8ers"

Output:
8

Explanation:
The number in the string is 8. There are no other numbers, so the sum is 8.