count digits

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2008
Posts: 29
Reputation: savinki is an unknown quantity at this point 
Solved Threads: 0
savinki savinki is offline Offline
Light Poster

count digits

 
0
  #1
Jun 3rd, 2008
Hi,

wt is the function that can use to count number of digits in a long integer?
e.g. 12345678 -> 8
11 ->2
456 -> 3
6724 -> 4
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: count digits

 
0
  #2
Jun 4th, 2008
You'll need to use the modulo (or remainder) operator: %

It does what it says:
cout << (123 % 10);

Hope this helps.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,842
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: count digits

 
0
  #3
Jun 4th, 2008
Originally Posted by savinki View Post
Hi,

wt is the function that can use to count number of digits in a long integer?
e.g. 12345678 -> 8
11 ->2
456 -> 3
6724 -> 4
I don't know that there is a function that does that. You can do two things though. You can use the log10 function from cmath:

http://www.cplusplus.com/reference/c...ath/log10.html

That function takes and returns doubles, so you'll need to do some typecasting and adjust by one.

Or you can set up a for-loop and divide by 10 each time through the loop using integer division. Count how many times you go through the loop.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,669
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 123
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: count digits

 
0
  #4
Jun 4th, 2008
or....
  1. int getNumDecimalDigits(int value)
  2. {
  3. char myString[16];
  4. sprintf(myString,"%d",value);
  5. return strlen(myString);
  6. }



.
Last edited by jephthah; Jun 4th, 2008 at 2:21 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 2
Reputation: deepak1331 is an unknown quantity at this point 
Solved Threads: 0
deepak1331 deepak1331 is offline Offline
Newbie Poster

Re: count digits

 
-1
  #5
Jun 4th, 2008
Originally Posted by savinki View Post
Hi,

wt is the function that can use to count number of digits in a long integer?
e.g. 12345678 -> 8
11 ->2
456 -> 3
6724 -> 4
Hey Saviniki try this code:
#include<stdio.h>
void main()
{
long int a;
int count=0,;
printf("Enter a interger :");
scanf("%d",&a);
while(a>0)
{
a=a/10;
count++;
}
printf("No. of digits in the no entered : %d",count);
}
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 248
Reputation: hammerhead is an unknown quantity at this point 
Solved Threads: 24
hammerhead's Avatar
hammerhead hammerhead is offline Offline
Posting Whiz in Training

Re: count digits

 
0
  #6
Jun 4th, 2008
@Deepak
Do not give away the solution. Read the last point of this link http://www.daniweb.com/forums/thread78223.html

Also posting C code in C++ forum is not a good idea that too without code tags.
Last edited by hammerhead; Jun 4th, 2008 at 4:37 am.
There are 10 types of people in the world, those who understand binary and those who don't.

All generalizations are wrong. Even this one.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1,491
Reputation: William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of 
Solved Threads: 123
Sponsor
William Hemsworth William Hemsworth is offline Offline
Nearly a Posting Virtuoso

Re: count digits

 
-1
  #7
Jun 4th, 2008
int getNumDecimalDigits(int value)
{
char myString[16];
sprintf(myString,"%d",value);
return strlen(myString);
}
That is not the best way to do it, heres a better way:

  1. // Will work with negative numbers
  2. int CountDidgets(int val) {
  3. int d = 1, c;
  4. if (val >= 0) for (c = 10; c <= val; c *= 10) d++;
  5. else for (c = -10 ; c >= val; c *= 10) d++;
  6. return (c < 0) ? ++d : d;
  7. }
I need pageviews! most fun profile ever :)
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 351
Reputation: Radical Edward has a spectacular aura about Radical Edward has a spectacular aura about Radical Edward has a spectacular aura about 
Solved Threads: 62
Radical Edward's Avatar
Radical Edward Radical Edward is offline Offline
Posting Whiz

Re: count digits

 
0
  #8
Jun 4th, 2008
> That is not the best way to do it
There's no such thing as the best overall way to do it. Your way might be better in Edward's program but worse in jephthah's, or vice versa. Though jephthah's function has the benefit of simplicity.

> heres a better way
The results are identical for the most part. Ed would argue that the sign has no business being counted as a digit, but that's something that depends on the needs of the program as well. One big problem with your better way is that it fails to handle INT_MIN and INT_MAX.

Edward would do something more like this for the counting method:
  1. int CountDigits(int value)
  2. {
  3. int count = 0;
  4.  
  5. value = std::abs(value);
  6.  
  7. do {
  8. ++count;
  9. value /= 10;
  10. } while (value != 0);
  11.  
  12. return count;
  13. }
Last edited by Radical Edward; Jun 4th, 2008 at 3:40 pm.
If at first you don't succeed, keep on sucking until you do succeed.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1,491
Reputation: William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of 
Solved Threads: 123
Sponsor
William Hemsworth William Hemsworth is offline Offline
Nearly a Posting Virtuoso

Re: count digits

 
0
  #9
Jun 4th, 2008
The results are identical
Not exactly, the function I made is Much more efficient and doesnt make any calls to any other functions. And I think the minus sign should count as a didget as if you are don't, trying to assign it to a char buffer and it wont have enough space if it's a negative number.
I need pageviews! most fun profile ever :)
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 351
Reputation: Radical Edward has a spectacular aura about Radical Edward has a spectacular aura about Radical Edward has a spectacular aura about 
Solved Threads: 62
Radical Edward's Avatar
Radical Edward Radical Edward is offline Offline
Posting Whiz

Re: count digits

 
0
  #10
Jun 4th, 2008
> the function I made is Much more efficient
Edward would argue that an infinite loop for some inputs isn't much more efficient. Ed would also argue that any sane programmer will use a slow and correct function rather than a fast and broken function. Finally, pushing for efficiency at the cost of clarity without bottleneck benchmarks to back up the decision is usually a bad idea.

> trying to assign it to a char buffer and it wont have enough space if it's a negative number
That makes sense if all you use this function for is allocating buffers. There are other uses as well, and counting the sign would be treated as an error in many of them. I think you should avoid counting the sign and let the caller decide what to do:
  1. int n = CountDigits(value);
  2.  
  3. if (value < 0 || includeSign)
  4. ++n;
  5.  
  6. ...
But that's just a personal preference. As long as your choice is well documented, the caller can do the opposite just as easily:
  1. int n = CountDigits(value);
  2.  
  3. if (value < 0)
  4. --n;
  5.  
  6. ...
Last edited by Radical Edward; Jun 4th, 2008 at 4:17 pm.
If at first you don't succeed, keep on sucking until you do succeed.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 3854 | Replies: 14
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC