count digits in string

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Aug 2008
Posts: 31
Reputation: timb89 is an unknown quantity at this point 
Solved Threads: 0
timb89 timb89 is offline Offline
Light Poster

count digits in string

 
0
  #1
Jun 18th, 2009
  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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,656
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1500
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: count digits in string

 
0
  #2
Jun 19th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,969
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: count digits in string

 
0
  #3
Jun 19th, 2009
Originally Posted by Ancient Dragon View Post
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
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,128
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 283
Moderator
WaltP's Avatar
WaltP WaltP is online now Online
Posting Sensei

Re: count digits in string

 
0
  #4
Jun 19th, 2009
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.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,969
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: count digits in string

 
0
  #5
Jun 19th, 2009
Originally Posted by WaltP View Post
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.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 360
Reputation: jencas is just really nice jencas is just really nice jencas is just really nice jencas is just really nice jencas is just really nice 
Solved Threads: 69
jencas jencas is offline Offline
Posting Whiz

Re: count digits in string

 
1
  #6
Jun 19th, 2009
And why 49 and 57 instead of '0' and '9'??? Are you training for an obfuscation contest?
If you are forced to reinvent the wheel at least try to invent a better one!

Please use code tags - Please mark solved threads as solved
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,969
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: count digits in string

 
0
  #7
Jun 19th, 2009
Originally Posted by jencas View Post
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.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 22
Reputation: Alicito is an unknown quantity at this point 
Solved Threads: 0
Alicito Alicito is offline Offline
Newbie Poster

Re: count digits in string

 
0
  #8
Jun 19th, 2009
I would solve it in this efficient easy way
  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
él¡é
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 360
Reputation: jencas is just really nice jencas is just really nice jencas is just really nice jencas is just really nice jencas is just really nice 
Solved Threads: 69
jencas jencas is offline Offline
Posting Whiz

Re: count digits in string

 
0
  #9
Jun 19th, 2009
Originally Posted by tux4life View Post
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.
If you are forced to reinvent the wheel at least try to invent a better one!

Please use code tags - Please mark solved threads as solved
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,656
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1500
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: count digits in string

 
0
  #10
Jun 19th, 2009
Originally Posted by tux4life View Post
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.
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: 697 | Replies: 9
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC