Discuss Finding the number of digits in the C Programming forum on Dev Shed. Finding the number of digits C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
Posts: 10
Time spent in forums: 2 h 23 m 45 sec
Reputation Power: 0
Finding the number of digits
Im trying to write a function in which you pass an integer as an argument and you get the number of digits that the integer contains back. Here is what I have, but the code is flawed:
int getsize(int x)
{
int n=1;
for(;
{
if(x%pow(10,n)==0)
return n;
n++;
}
}
I havent tested it out, but the most obvious flaw is that if the integer is a multiple of 10, then x%10 will give 0 which will terminate the loop prematurely.
Can someone point out which way I should head to get this function working? I can do the code on my own.
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,406
Time spent in forums: 2 Months 9 h 47 m 28 sec
Reputation Power: 4080
The log10() thing won't work for negative numbers or 0 though. You could get around this by using:
(a) Check for 0 and return 1
(b) Check for - sign and then convert the number to positive and find its length and add 1 to it (for the - sign)
Another way would be to use the sprintf() function, which returns the number of characters that were used in the string:
Code:
char buf[30];
int i = 1234;
int len = sprintf(buf, "%d", i);
__________________ Up the Irons
What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home. "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
Down with Sharon Osbourne
Posts: 4,824
Time spent in forums: 1 Month 2 Days 21 h 1 m
Reputation Power: 1800
Quote:
Originally Posted by Scorpions4ever
(b) Check for - sign and then convert the number to positive and find its length and add 1 to it (for the - sign)
Or just use abs(x) as the argument to log10() as requinix did in his original suggestion. Your solution (both of them) counts the sign as a digit for negative numbers. It is not a digit, and counting it as such may not be the intention, it is certainly not what was asked.
If performance were an issue sprintf() is probably far slower.
Posts: 3,905
Time spent in forums: 2 Months 3 Weeks 4 Days 1 h 9 m 41 sec
Reputation Power: 1774
Logarithms are not exactly cheap either.
Code:
#include <stdio.h>
int intlen ( int a ) {
int len = 0;
if ( a < 0 ) a = -a;
while ( a >= 10000 ) { len += 4; a /= 10000; }
while ( a >= 100 ) { len += 2; a /= 100; }
while ( a >= 10 ) { len ++; a /= 10; }
return len + 1;
}
int main ( ) {
printf("%d\n", intlen(0) );
printf("%d\n", intlen(9) );
printf("%d\n", intlen(42) );
printf("%d\n", intlen(123456789) );
return 0;
}
$ ./a.exe
1
1
2
9
The string algorithm does not benefit much from optimisation. Salem's solution is obviously superior.
Note I changed the floor() call to a simple cast to int in my version. This has a significant beneficial effect on execution time. Note also the cast to double, this is to disambiguate from the single precision overload in C++ compilation. If you cast to float as I originally did, you get the wrong answer for 7 digit values close to 10,000,000 (this is what the checksum was there for - to make sure they produced the same result).
It is interesting because I have in the past and would have in this case, suggested the log10 method. It remains easy to implement (and remember) and is reasonably fast on a system with an FPU at least. Since I generally do embedded systems development and have no FPU, I'll have to remember Salem's solution!)