Problem 57. Summing Digits within Text
Given a string with text and digits, add all the numbers together.
Examples:
Input str = '4 and 20 blackbirds baked in a pie' Output total is 24
Input str = '2 4 6 8 who do we appreciate?' Output total is 20
Solution Stats
Problem Comments
-
3 Comments
Jean-Marie Sainthillier
on 19 Jul 2012
Very nice !
Dariusz
on 3 Feb 2013
This problem shouldn't be named "Summing digits...." because it is the wrong approach. In the first example you sum numbers because 20 is not a digit. If it was - then it should be sum = 6.
Carl Witthoft
on 18 Nov 2015
What if there are decimals "I have $3.25 in 4 pockets" ? Should the answer be 7.25 or 32?
Solution Comments
Show commentsProblem Recent Solvers2497
Suggested Problems
-
Return the largest number that is adjacent to a zero
5332 Solvers
-
3358 Solvers
-
Sum of first n terms of a harmonic progression
416 Solvers
-
Matrix indexing with two vectors of indices
718 Solvers
-
Convert from Fahrenheit to Celsius
23485 Solvers
More from this Author96
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!