How to add the digits of a variable, either string or int?! :S

Hello friends. I am coding again after a long sabbatical, but that is not the reason I am ignorant to this simple operation. I don’t recall ever knowing how to do this, so if you would please help me know moar I shall grin at my monitor and high-five a wall in your honor upon answering my question (provided said wall isn’t made of deathly spikes or magma).

Example:
// Program Monitors Serial in for a string, (multiple words separated by spaces), then combs through said string to add values together and print out.

input: CAT DOG IS GREAT

machine sees: 312 467 91 79512 (assuming variables are assigned giving letters their numerology values, a=1, b=2, etc)

I have faith this programmer can: Split into 4 variables for each word by checking for spaces, assuming this is done, then I wish to compute as such:

var1(CAT) becomes 6, var2(DOG)becomes 8 (not 17!), var3(IS) becomes 1 (not 10!), var 4(GREAT) becomes 6 (not 24!)

output (var1 + var2 + var3 + var4): 6 8 1 6

Is this of the doable spectrum of programming ability?!?! If it is I would be so very well on my way with your help! I’ve been trying to implement something like this (albeit more complex) in Java for a week now and finally rage quitted to work with my favorite, most familiar code. Enter the Arduino - A Bamf Micro-controller tasked with crunching numbers while plugged into my number crunching machine (desktop PC) Lol.

I know how to do most of the stuff this program would entail I believe, just have no idea how to take a string variable in the format word1(space)word2(space)word3, convert it into x #of variables depending on how many words (or spaces more specifically), and then add the values of each variable individually using pre-defined values for the characters of my choosing, mainly A-Z assigned to 1-9.

If all of this is above you, heck, I’m like a used car salesman, you could pay my wage by simply offering me the knowledge of how to add digits of a variable together. Desperation showing through the monitor at all?

Then let me make a final elaboration of my feelings. So tricky this is for I, and for reading this far into this without closing me off I commend you and offer you an Uncertified Arduino Forum Achievement by email, should you request. Seriously you are helping me do something I’ve been stumped on far too long. I have no more limbs to lose (save my sanity-limb more familiarly known as my mind)!

With all the love in my heart,
I’m going to go smoke a cig.
<3Blake

Look each char typed on you keyboard is sent as a value in a code known as ASCII thats your starting point. Stay off the strange baccy.

Mark

var1(CAT) becomes 6, var2(DOG)becomes 8 (not 17!), var3(IS) becomes 1 (not 10!), var 4(GREAT) becomes 6 (not 24!)

Please explain how the values for the vars are derived.

Well, finding the individual characters of a sting is pretty easy. If you have char foo = "CAT"; then foo[0] is 'C', foo[1] is 'A', and foo[2] is 'T'. 67, 65, 84 if you haven't done anything special. You can subtract 64 from each character to get 1 for 'A', if that's what you had in mind.

Now, if you have an integer int bar = 12345;, the situation is more complicated. The "digits" of an integer are something that only shows up when the integer is printed (or converted to a string), so extracting the individual digits is relatively complicated (indeed, the easier way may be to convert the variable to a string first, and then just deal with the string.)

Note that to convert "DOG" to 8, you would need to do both. First you'd add up the characters of the string (giving you an integer), and then you have to add up the digits of the integer (possibly more than once, till there was only one digit left.)

UKHeliBob: Please explain how the values for the vars are derived.

Also, why are you doing this on an Arduino? It seems like a simple (although bizarre) text processing problem. You could solve it just as easily in Java, C++ or whatever other language you feel most comfortable with and it would be easier to program on a PC than on an Arduino. There's really nothing here that the Arduino is helping with, and it means you're doing your development in a very limited environment.

westfw: Well, finding the individual characters of a sting is pretty easy. If you have char foo = "CAT"; then foo[0] is 'C', foo[1] is 'A', and foo[2] is 'T'. 67, 65, 84 if you haven't done anything special. You can subtract 64 from each character to get 1 for 'A', if that's what you had in mind.

Now, if you have an integer int bar = 12345;, the situation is more complicated. The "digits" of an integer are something that only shows up when the integer is printed (or converted to a string), so extracting the individual digits is relatively complicated (indeed, the easier way may be to convert the variable to a string first, and then just deal with the string.)

Note that to convert "DOG" to 8, you would need to do both. First you'd add up the characters of the string (giving you an integer), and then you have to add up the digits of the integer (possibly more than once, till there was only one digit left.)

I chose to reply to you first (though I am getting to those of you who are classified as an "other of you" next!), because it sounds like we are really getting somewhere here with this. All I know is that I do not know of this mysterious foo, and that it might perhaps help me. :)

In fact, I was about to inquire further for understanding, but upon reading through one more time, this makes perfect sense to me and is most helpful indeed! Believe it or not, I already wrote a non-functioning program in Java in which I was able to write the bit that compresses and re-compresses until one digit with great success. However... there was one pseudo command that checks if variable (integer) is a single digit. I assume there is some command in the Arduino language for this too? If not are there any multiplication/division/add or sub tricks I can use to figure out if I have an int with a single dig? I thought about the "roll-over" of a variable, but concluded the value at which a variable rolls over is not configurable and hence won't help me. When I get all of this working I will post my code by the way, because I believe with what you just enlightened me on, and providing I can find a solution to my "is x single digit" problem, then this code shall finally be completed.

UKHeliBob:

var1(CAT) becomes 6, var2(DOG)becomes 8 (not 17!), var3(IS) becomes 1 (not 10!), var 4(GREAT) becomes 6 (not 24!)

Please explain how the values for the vars are derived.

Simply enough, I assign the values for each character in the init. If you take your 26 letter alphabet, and upon reaching any letter/number over 9, add the digits together until there is a single digit, you will get where I take my defined values from. In this sense I set A B C to 1 2 3 onward, and upon reaching J K L I set them 1 2 3 as opposed to 10 11 12.

I guess I'm iffy though on whether or not I can take a word and store it in some array somewhere as the predefined values rather than the word itself. This can be simply avoided though by storing the input string as several strings one letter long, I suppose. ie maybe the machine can't interpret ABCDE as 12345 when storing the variable in an array, but it may be able to interpret A as 1, B as 2, onward. Will post my results. While the program is of no use to anyone, I imagine a newb or two like myself could gleam some insight from a results post that describes variables and the various ways to store them and break them apart of use. I for one can tell you that it seems the arduino learning database had a lot more information on them starting out, because in my research with those materials I often had questions that weren't answered anywhere in the helpful reference.

holmes4: Look each char typed on you keyboard is sent as a value in a code known as ASCII thats your starting point. Stay off the strange baccy.

Mark

Baccy is wacky indeed! I meant acig (artificial communication intelligence genesis) though, which is a project of mine I have been trying to crack for months and mentioned a few times in these parts. Perhaps my subconscious snuck the space in there because it was trying to tell me something (my, user, you are all strung out! :P) I think the fact that I used the verb smoke had something to do with the unintentional spelling blip too. Strangely enough I've never thought about my program that way.

PeterH: Also, why are you doing this on an Arduino? It seems like a simple (although bizarre) text processing problem. You could solve it just as easily in Java, C++ or whatever other language you feel most comfortable with and it would be easier to program on a PC than on an Arduino. There's really nothing here that the Arduino is helping with, and it means you're doing your development in a very limited environment.

This question I am most glad of all to answer. While you make a fantastic point, I have two reasons. The first, and foremost, I actually mentioned in my original post:

I've been trying to implement something like this (albeit more complex) in Java for a week now and finally rage quitted to work with my favorite, most familiar code. Enter the Arduino - A Bamf Micro-controller tasked with crunching numbers while plugged into my number crunching machine (desktop PC) Lol.

The second reason was left undisclosed as I find a post with too much text and information often makes it far more difficult for others to assist with, and not only that but has the side-effects of causing the topic to stray, and those who would've replied were the question precise and short, might decide against it upon being confronted with a wall of "oh my gerd".

This second reason for why I am using the Arduino, is that later (as in when I tackle the things at the top of my to-do list) I plan on enclosing a 'duino with an lcd screen and qwerty keypad so that I can munch down numbers and spare myself from doing maths on the go! It should be pretty neat (for me at least, who has use for such a gadget). I look forward to sharing more with anyone interested when the time comes... because what good is a project if left unshared.

And that's perhaps what I love about the Arduino community the most. We are all working on often insane and very useless things and do so because to us (and often times for those we share our work with) the sillyness and lack of purposeful meaning is downright cool. I think you can find an Arduino behind the scenes of almost any creative art project, or strange piece, and that is a testament to just how flexible and easy to pick up the platform is.

Thank you everyone! As I mentioned above, I will be sharing my finished code and elaborating on those details I learned of to complete it that I found lacking in our helpful language reference. -Blake

command that checks if variable (integer) is a single digit.

How about:

if (myinteger > 9) {
  // code for integer with more than one digit (greater than 9)
} else {
  // code for single digit integer.
}