| | |
count digits in string
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Aug 2008
Posts: 31
Reputation:
Solved Threads: 0
C++ Syntax (Toggle Plain Text)
int countDigits(string word) { int count = 0; for (int i = 0; i < word.size(); i++) { if (word.at(i) < 57 && word.at(i) > 49) { count++; } } }
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.
•
•
•
•
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]) )
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."
Change your comparison to
Also, the line is better written as
It's a more logical order for the comparison pair.
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
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
•
•
•
•
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.
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."
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."
•
•
Join Date: Apr 2009
Posts: 22
Reputation:
Solved Threads: 0
I would solve it in this efficient easy way
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> using namespace std; int countdigits(string word) { int count=0; for(int i=0;i<(int)word.length();i++) { if(isdigit(word[i])) count=count+1; } return count; } void main() { string word; getline(cin,word); int digits=0; digits=countdigits(word); cout<< digits<<endl; }
Last edited by Ancient Dragon; Jun 19th, 2009 at 10:19 am. Reason: add code tags, remove color and bold tags
él¡é
•
•
Join Date: Dec 2007
Posts: 360
Reputation:
Solved Threads: 69
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
Please use code tags - Please mark solved threads as solved
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.
![]() |
Similar Threads
- count digits (C++)
- count characters in a string (Perl)
- Count characters of a string. (C++)
- help with c program to count # of words in a string (C)
Other Threads in the C++ Forum
- Previous Thread: Idl compilation
- Next Thread: Draw shapes in C++
Views: 697 | Replies: 9
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linux loop looping loops map math matrix memory microsoft newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






