| | |
Count characters of a string.
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
Hi,
I want to count the number of characters of a string. Try to do it as follows.
Can you guys comments on my code, either this way is ok or bad attempt
I want to count the number of characters of a string. Try to do it as follows.
C++ Syntax (Toggle Plain Text)
int iSpace = 0; int iLength; string strTemp("This is a line of text"); iLength = strTemp.length(); cout << iLength << endl; for(int i = 0; i < iLength; i++) { if((strTemp.at(i) == ' ') || (strTemp.at(i) == '\t') || (strTemp.at(i) == '\0')) { iSpace ++; } } printf("\nNumber of characters %d", (iLength - iSpace));
Can you guys comments on my code, either this way is ok or bad attempt
Last edited by eranga262154; Dec 18th, 2007 at 4:45 am. Reason: Adding more
“victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha
Use isspace for a check like this.
cpp Syntax (Toggle Plain Text)
for(int i = 0; i < iLength; i++) { if(!isspace(strTemp.at(i))) { iSpace++; } }
Correction to WolfPack post....
C++ Syntax (Toggle Plain Text)
for(int i = 0; i < iLength; i++){ if(isspace(strTemp.at(i))) { iSpace ++; } }
Last edited by neosomosis; Dec 18th, 2007 at 5:51 am. Reason: typo
![]() |
Similar Threads
- Counting all Non-blank Characters in a String (Java)
- why does my string array do this? (C++)
- Counting specific characters in a string (C)
- Character/letter count (Pascal and Delphi)
- Ways to create Dictionary (Python)
- How DO you count Upper Case letters? (Java)
- C++ BASICS ==> Pointers, Call by Reference/Value, Inheritance, Functions & Arrays (C++)
- String operatios (C++)
Other Threads in the C++ Forum
- Previous Thread: Doubt in Native API
- Next Thread: Message
Views: 13189 | Replies: 18
| 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






