943,855 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1675
  • C++ RSS
Jun 18th, 2009
0

count digits in string

Expand Post »
C++ Syntax (Toggle Plain Text)
  1. int countDigits(string word)
  2. {
  3. int count = 0;
  4.  
  5. for (int i = 0; i < word.size(); i++)
  6. {
  7. if (word.at(i) < 57 && word.at(i) > 49)
  8. {
  9. count++;
  10. }
  11. }
  12. }

i want to count the digits in a string "abc123". i know my problem is in the if statemnt i just cant get it working

stupid question i know im just stuck
Last edited by timb89; Jun 18th, 2009 at 11:43 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
timb89 is offline Offline
56 posts
since Aug 2008
Jun 19th, 2009
0

Re: count digits in string

you can use the macro isdigit() to find out if it is '0' to '9'. And the At() method is unnecessary.

if( isdigit(word[i]) )
Last edited by Ancient Dragon; Jun 19th, 2009 at 12:18 am.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,951 posts
since Aug 2005
Jun 19th, 2009
0

Re: count digits in string

you can use the macro isdigit() to find out if it is '0' to '9'. And the At() method is unnecessary.

if( isdigit(word[i]) )
Is isdigit a macro then?
According to this information it isn't
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
Jun 19th, 2009
0

Re: count digits in string

Change your comparison to
if (word[i] < 57 && word[i] > 49)

Also, the line is better written as
if (word[i] > 49 && word[i] < 57)
It's a more logical order for the comparison pair.
Moderator
Reputation Points: 3278
Solved Threads: 892
Posting Sage
WaltP is offline Offline
7,718 posts
since May 2006
Jun 19th, 2009
0

Re: count digits in string

Click to Expand / Collapse  Quote originally posted by WaltP ...
Change your comparison to
if (word[i] < 57 && word[i] > 49)

Also, the line is better written as
if (word[i] > 49 && word[i] < 57)
It's a more logical order for the comparison pair.
Yes, but isn't the isdigit function a bit easier to work with?
However if he isn't allowed to use that function it would seem more logical to me if he'd written it like you did
Last edited by tux4life; Jun 19th, 2009 at 3:30 am.
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
Jun 19th, 2009
1

Re: count digits in string

And why 49 and 57 instead of '0' and '9'??? Are you training for an obfuscation contest?
Reputation Points: 395
Solved Threads: 71
Posting Whiz
jencas is offline Offline
362 posts
since Dec 2007
Jun 19th, 2009
0

Re: count digits in string

Click to Expand / Collapse  Quote originally posted by jencas ...
And why 49 and 57 instead of '0' and '9'??? Are you training for an obfuscation contest?
Agreed, '0' and '9' makes your code more readable, but the OP started with using the ASCII codes
But this is actually not a good reason to avoid this I think
Last edited by tux4life; Jun 19th, 2009 at 4:01 am.
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
Jun 19th, 2009
0

Re: count digits in string

I would solve it in this efficient easy way
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. int countdigits(string word)
  6. {
  7. int count=0;
  8. for(int i=0;i<(int)word.length();i++)
  9. {
  10. if(isdigit(word[i]))
  11. count=count+1;
  12. }
  13. return count;
  14. }
  15. void main()
  16. {
  17. string word;
  18. getline(cin,word);
  19. int digits=0;
  20. digits=countdigits(word);
  21. cout<< digits<<endl;
  22. }
Last edited by Ancient Dragon; Jun 19th, 2009 at 10:19 am. Reason: add code tags, remove color and bold tags
Reputation Points: 37
Solved Threads: 0
Light Poster
Alicito is offline Offline
33 posts
since Apr 2009
Jun 19th, 2009
0

Re: count digits in string

Click to Expand / Collapse  Quote originally posted by tux4life ...
Agreed, '0' and '9' makes your code more readable, but the OP started with using the ASCII codes
But this is actually not a good reason to avoid this I think
I didn't quote your answer because my answer was intended to address the TO.
Reputation Points: 395
Solved Threads: 71
Posting Whiz
jencas is offline Offline
362 posts
since Dec 2007
Jun 19th, 2009
0

Re: count digits in string

Click to Expand / Collapse  Quote originally posted by tux4life ...
Is isdigit a macro then?
According to this information it isn't
Its probably implementation dependent. You have to check your compiler's ctype.h header file to find out whether its implemented as a macro or a function. The link you posted doesn't say one way or the other.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,951 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Idl compilation
Next Thread in C++ Forum Timeline: Editing a file





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC