User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 402,049 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,522 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 680 | Replies: 14
Reply
Join Date: Apr 2008
Posts: 27
Reputation: savinki is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
savinki savinki is offline Offline
Light Poster

count digits

  #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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,836
Reputation: Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold 
Rep Power: 11
Solved Threads: 189
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: count digits

  #2  
Jun 3rd, 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  
Join Date: Jan 2008
Posts: 1,490
Reputation: VernonDozier is a jewel in the rough VernonDozier is a jewel in the rough VernonDozier is a jewel in the rough VernonDozier is a jewel in the rough 
Rep Power: 6
Solved Threads: 187
VernonDozier VernonDozier is offline Offline
Nearly a Posting Virtuoso

Re: count digits

  #3  
Jun 3rd, 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  
Join Date: Feb 2008
Location: Seattle
Posts: 713
Reputation: jephthah is a jewel in the rough jephthah is a jewel in the rough jephthah is a jewel in the rough 
Rep Power: 4
Solved Threads: 45
jephthah's Avatar
jephthah jephthah is offline Offline
Master Poster

Re: count digits

  #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 1:21 am.
Why so serious?
Reply With Quote  
Join Date: Dec 2007
Posts: 2
Reputation: deepak1331 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
deepak1331 deepak1331 is offline Offline
Newbie Poster

Re: count digits

  #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  
Join Date: May 2006
Location: India
Posts: 247
Reputation: hammerhead is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 24
hammerhead's Avatar
hammerhead hammerhead is offline Offline
Posting Whiz in Training

Re: count digits

  #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 3: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  
Join Date: Mar 2008
Location: UK
Posts: 374
Reputation: williamhemsworth is a jewel in the rough williamhemsworth is a jewel in the rough williamhemsworth is a jewel in the rough 
Rep Power: 3
Solved Threads: 37
williamhemsworth's Avatar
williamhemsworth williamhemsworth is offline Offline
Posting Whiz

Re: count digits

  #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. }
Approximately 298.60465116279069767441860465116 days until I get my first star x]
Reply With Quote  
Join Date: May 2008
Posts: 349
Reputation: Radical Edward has a spectacular aura about Radical Edward has a spectacular aura about Radical Edward has a spectacular aura about 
Rep Power: 3
Solved Threads: 59
Radical Edward's Avatar
Radical Edward Radical Edward is offline Offline
Posting Whiz

Re: count digits

  #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:
int CountDigits(int value)
{
  int count = 0;

  value = std::abs(value);

  do {
    ++count;
    value /= 10;
  } while (value != 0);

  return count;
}
Last edited by Radical Edward : Jun 4th, 2008 at 2:40 pm.
If at first you don't succeed, keep on sucking until you do succeed.
Reply With Quote  
Join Date: Mar 2008
Location: UK
Posts: 374
Reputation: williamhemsworth is a jewel in the rough williamhemsworth is a jewel in the rough williamhemsworth is a jewel in the rough 
Rep Power: 3
Solved Threads: 37
williamhemsworth's Avatar
williamhemsworth williamhemsworth is offline Offline
Posting Whiz

Re: count digits

  #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.
Approximately 298.60465116279069767441860465116 days until I get my first star x]
Reply With Quote  
Join Date: May 2008
Posts: 349
Reputation: Radical Edward has a spectacular aura about Radical Edward has a spectacular aura about Radical Edward has a spectacular aura about 
Rep Power: 3
Solved Threads: 59
Radical Edward's Avatar
Radical Edward Radical Edward is offline Offline
Posting Whiz

Re: count digits

  #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:
int n = CountDigits(value);

if (value < 0 || includeSign)
  ++n;

...
But that's just a personal preference. As long as your choice is well documented, the caller can do the opposite just as easily:
int n = CountDigits(value);

if (value < 0)
  --n;

...
Last edited by Radical Edward : Jun 4th, 2008 at 3:17 pm.
If at first you don't succeed, keep on sucking until you do succeed.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 11:44 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC