• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Sum of digits in alphanumeric string

 
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your reply.

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.
 
Ranch Hand
Posts: 754
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Raj Kumar Bindal wrote:Thanks for your reply.

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?
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"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.
 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic