Win a copy of Functional Reactive Programming this week in the Other Languages forum!

# Sum of digits in alphanumeric string

Raj Kumar Bindal
Ranch Hand
Posts: 418
I have an alphanumeric string. I need the sum of all of the digits in this string. What is the best and most efficient way to find the sum of all digits. Please suggest the solution which can be considered generic for all programming languages.

Kevin Workman
Ranch Hand
Posts: 151
The solution is going to change depending on which language you're using. Check out the String API for useful functions. You might also want to check out the Integer API.

Raj Kumar Bindal
Ranch Hand
Posts: 418

I know solution is going to change based on the programming language. That is why i wrote that solution should be irrespective of any language.

So, we do not have to use any function which we think will be specific to java.

Hebert Coelho
Ranch Hand
Posts: 754
The only way I can see is that you would have to transform each string in a int, and then sum it all. =/

Kevin Workman
Ranch Hand
Posts: 151

I know solution is going to change based on the programming language. That is why i wrote that solution should be irrespective of any language.

So, we do not have to use any function which we think will be specific to java.

The problem is, it's pretty difficult to create a solution "irrespective of any language" other than in pseudocode. For example, how would you do this in whitespace, or lolcode?

Paul Clapham
Sheriff
Posts: 21416
33
Raj Kumar Bindal wrote:Please suggest the solution which can be considered generic for all programming languages.

There isn't a solution which is language-independent. If you're working in a procedural C-like language, such as Java for example, then you would go through the string one character and a time, converting the characters to numbers and adding them up. Whereas in a functional language like Lisp, you would more likely write a recursive function which added the number corresponding to the first character of the string to the value of that same function applied to the rest of the string.

fred rosenberger
lowercase baba
Bartender
Posts: 12198
35
"Best" in terms of what - speed, memory, code complexity? Your question is meaningless without better defining what is most important.

Much of software design is a balance in the above mentioned items. If two people each think a different thing is the more important one, then their "best" solution will be vastly different.

And if you want a 'solution which can be considered generic for all programming languages', then that would be "sum all of the digits" - as you stated in your question.

Alex Hurtt
Ranch Hand
Posts: 98
There is an ambiguity in the stated problem it would seem to me...How do you handle multiple consecutive digits? Like so:

abc12defg34ijk

If you sum all the digits you could have

12 + 34 = 46

or do you want to treat it as

1 + 2 + 3 + 4 = 10

Which is the answer you expect?

Raj Kumar Bindal
Ranch Hand
Posts: 418
I will expect the answer like this:

1 + 2 + 3 + 4 = 10

We have to do some of all one digit numbers in the string.

Campbell Ritchie
Sheriff
Posts: 50241
79
You would have to go through the String, find each character, find out whether each character is a digit, find its numerical value, and add that.

There are methods in the String and Character classes for all those things, except adding.