I'm currently writing a small application that is supposed to calculate the digital root of any given number (Within certain bounds obviously).
At the moment i'm trying to get the logic right:
The number i will be using is inputted by the user from the command-line, i'll then simply loop through argv and add up all the numbers, but this is as far as i got. After i got the sum of all the digits, i need to allocate an array big enough to hold the result (With one digit in each int, so i can work with them one digit at a time). This means, i have to figure out the maximum possible value of the sum of the inputted numbers, this in itself is not hard (Size of argv * 9, since 9 is the highest single digit number), but then i need to figure out how many digits this new number has, and allocate the appropriate amount of memory.
So far i've been doing this:
Say the user inputs a number with 12 digits on the cmd line, i know that no matter what was inputted, the resulting number after adding them all, can't be bigger than 12*9, so i just allocated an array of [12*9], but this isn't what i really wan't, this is actually way more space than i need, and i just realized this.
In this example i won't need more than 3 int's, because the resulting number is 108, but how would i go about figuring this out in C? How can i do that mathematically?
Oh, and another small question, how can i check if the user inputted only numbers, or decided to put some letters in there for whatever reason? Is there a function similar to isalpha() that will do this, e.g. isnumber()?