Sum of All Numbers in a String: 4 Language Solutions
Step-by-step programs in Python, Java, C, and C++ to extract every number from a string and return their sum, with worked examples and complexity notes.
Given a string that mixes letters and digits, find the sum of every embedded integer, treating 'abc45def5ghi32' as 45 + 5 + 32 = 82 rather than as individual digits.
This is a standard string-parsing problem that appears in campus drives and online coding rounds. The core logic fits in under 15 lines in any language. The edge cases (multi-digit numbers, consecutive digits, strings with no digits, leading zeros) are what interviewers probe.
Understanding the Problem
The input is a string that can contain letters, digits, and sometimes special characters. The output is a single integer: the sum of all contiguous digit sequences, where each sequence is interpreted as one number.
Worked Example
- Input:
abc45def5ghi32 - Numbers found: 45, 5, 32
- Output: 45 + 5 + 32 = 82
Edge Cases to Account For
- No digits:
abcdefgives output 0 - All digits:
12345gives output 12345 (one number, not 1+2+3+4+5) - Leading zeros:
a007bgives 007 = 7 - Adjacent numbers separated by letters:
a1b2c3gives 1 + 2 + 3 = 6 - Numbers at the start or end:
42abc99gives 42 + 99 = 141
A clean mental model: scan left to right, accumulate digits into a running number, and flush that running number into the total sum every time you hit a non-digit character (or the end of the string).
Algorithm and Approach
The scan-and-accumulate pattern works in O(n) time with O(1) space:
Initialize sum = 0, current_number = 0
For each character c in the string:
If c is a digit:
current_number = current_number * 10 + digit_value(c)
Else:
sum = sum + current_number
current_number = 0
After the loop ends:
sum = sum + current_number (flush the last number if string ends on a digit)
Return sum
The multiply-by-10 step converts a stream of digit characters into a proper integer. Without it, '45' becomes 4 + 5 = 9 instead of 45.
An alternative is the regex approach: extract all contiguous digit sequences as strings, convert each to an integer, and sum the list. This is idiomatic Python and Java, though it trades O(1) space for O(k) space where k is the count of numbers found.
Programs in Python, Java, C, and C++
Python
Two approaches: manual scan and regex. Both are correct; the regex version is shorter.
# Manual scan — O(n) time, O(1) space
def sum_of_numbers_in_string(s):
total = 0
current = 0
for ch in s:
if ch.isdigit():
current = current * 10 + int(ch)
else:
total += current
current = 0
total += current # flush last number
return total
# Regex approach — O(n) time, O(k) space
import re
def sum_of_numbers_regex(s):
numbers = re.findall(r'\d+', s)
return sum(int(n) for n in numbers)
# Test
s = "abc45def5ghi32"
print(sum_of_numbers_in_string(s)) # 82
print(sum_of_numbers_regex(s)) # 82
The Python re module documentation covers re.findall in detail. The pattern r'\d+' matches one or more consecutive digit characters, returning a list of strings like ['45', '5', '32'].
Java
Java’s Pattern class mirrors the Python regex approach. The manual scan also ports directly.
import java.util.regex.*;
public class SumOfNumbersInString {
// Manual scan
public static int sumManual(String s) {
int total = 0, current = 0;
for (char c : s.toCharArray()) {
if (Character.isDigit(c)) {
current = current * 10 + (c - '0');
} else {
total += current;
current = 0;
}
}
total += current;
return total;
}
// Regex approach
public static int sumRegex(String s) {
Pattern p = Pattern.compile("\\d+");
Matcher m = p.matcher(s);
int total = 0;
while (m.find()) {
total += Integer.parseInt(m.group());
}
return total;
}
public static void main(String[] args) {
String s = "abc45def5ghi32";
System.out.println(sumManual(s)); // 82
System.out.println(sumRegex(s)); // 82
}
}
C
C has no built-in regex in the standard library (POSIX regex.h exists on Linux but is not portable to all environments). The manual scan is the standard approach.
#include <stdio.h>
#include <ctype.h>
int sumOfNumbers(const char *s) {
int total = 0, current = 0;
while (*s) {
if (isdigit(*s)) {
current = current * 10 + (*s - '0');
} else {
total += current;
current = 0;
}
s++;
}
total += current; /* flush last number */
return total;
}
int main() {
printf("%d\n", sumOfNumbers("abc45def5ghi32")); /* 82 */
return 0;
}
C++
The C approach ports directly to C++. An idiomatic C++ version using std::string iterators looks the same except for the loop syntax.
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int sumOfNumbers(const string& s) {
int total = 0, current = 0;
for (char c : s) {
if (isdigit(c)) {
current = current * 10 + (c - '0');
} else {
total += current;
current = 0;
}
}
total += current;
return total;
}
int main() {
cout << sumOfNumbers("abc45def5ghi32") << endl; // 82
return 0;
}
Complexity Analysis
All four implementations share the same asymptotic profile:
| Approach | Time | Space |
|---|---|---|
| Manual scan (C, C++, Java, Python) | O(n) | O(1) |
| Regex (Python, Java) | O(n) | O(k) where k = count of numbers found |
The manual scan visits each character exactly once and keeps only two integer variables (total, current). The regex approach stores all matched substrings before summing, so space scales with the number of distinct integers in the input.
For placement rounds, either complexity claim is acceptable. Interviewers check whether you understand the trade-off, not which one you pick.
If you want to explore a related problem (extracting and summing individual digits rather than full numbers), the digit-sum program on FACE Prep covers that variant separately.
Placement Round Context
This problem appears in campus drives at companies that run written coding tests in the first round. CoCubes (now part of Wheebox) and Infosys Hackathon-style tests have included string-parsing variants of this type. The pattern is consistent: the question gives a string with interleaved digits and letters and asks for a single integer output.
Input sizes in these rounds typically fit well within O(n) reach. The time constraint is rarely the bottleneck; correctness on multi-digit numbers and edge cases (empty string, string with no digits) is where points are lost.
GeeksforGeeks covers this problem with additional variants. The section on negative-number handling is worth reading if your target company’s previous papers include signed integers.
Common Variants You Should Know
- Replace every digit in the string with 0 — same scan logic, different action at the digit branch.
- Count of distinct numbers in the string — use a set instead of a sum.
- Sum of numbers at even positions only — add an index counter to the scan.
- Largest number embedded in the string — track max instead of sum.
Understanding the space complexity of each variant matters when the input string can be long. The O(1)-space manual scan is preferable to the regex approach for memory-constrained interview questions, where interviewers often ask “can you do this without a list?”
The symmetric-pairs-in-array problem is another placement-round classic that pairs well with string parsing: both require scanning a data structure systematically while maintaining a running state.
String parsing runs through all of this work. The same O(n) scan that finds numbers in a string is the core logic behind log parsing, CSV field extraction, and structured-data processing. If you want to move from interview prep to building with these patterns, TinkerLLM starts exactly here: given a string of mixed data, extract what you need and process it. The entry cost is ₹299.
Primary sources
Frequently asked questions
What is the difference between sum of digits and sum of numbers in a string?
Sum of digits treats every character individually — '45' contributes 4 + 5 = 9. Sum of numbers in a string treats contiguous digit sequences as a single integer — '45' contributes 45. This problem is the second type.
Does the algorithm handle multi-digit numbers correctly?
Yes, if you accumulate digits before adding. When you see a digit, multiply the running number by 10 and add the new digit. When you hit a non-digit (or end of string), add the accumulated number to the total sum and reset the accumulator.
What happens if the string contains no numbers at all?
The sum stays 0, which is the correct return value. The loop completes without ever adding to sum. Both the manual scan and the regex approach handle this case without needing a special branch.
Can the numbers in the string be negative?
Standard placement-round variants treat all numbers as non-negative. If a minus sign can appear before a digit sequence, you need to check whether the preceding character is a minus and handle its sign separately. The programs here cover the standard positive-only case.
How does Python re.findall compare to a manual scan for this problem?
re.findall(r'\d+', s) is more concise and less error-prone than a manual loop. The manual scan is better for C and C++ (where re is not available by default) and is worth knowing for interviews where you must explain the logic step by step.
What is the time complexity of this algorithm?
O(n) where n is the length of the string. Each character is visited exactly once. Space complexity is O(1) if you accumulate numbers in place, or O(k) if you store the extracted numbers in a list first, where k is the count of numbers found.
Does the algorithm handle numbers with leading zeros?
Yes. '007' is treated as the integer 7 naturally, because you build the number by multiplying by 10 and adding each digit (0*10+0=0, 0*10+7=7). The regex approach also returns '007' as a string, which Python converts to 7 via int('007').
A self-paced playground for building with LLMs.
TinkerLLM is FACE Prep's sister property. A guided environment for shipping real LLM applications, the kind of project that earns a paragraph on your resume, not a line.
Try TinkerLLM (₹299 launch)