Count characters of a string.

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

Join Date: Sep 2007
Posts: 185
Reputation: eranga262154 is an unknown quantity at this point 
Solved Threads: 2
eranga262154's Avatar
eranga262154 eranga262154 is offline Offline
Junior Poster

Count characters of a string.

 
0
  #1
Dec 18th, 2007
Hi,

I want to count the number of characters of a string. Try to do it as follows.

  1. int iSpace = 0;
  2. int iLength;
  3.  
  4. string strTemp("This is a line of text");
  5. iLength = strTemp.length();
  6.  
  7. cout << iLength << endl;
  8.  
  9. for(int i = 0; i < iLength; i++)
  10. {
  11. if((strTemp.at(i) == ' ') || (strTemp.at(i) == '\t') || (strTemp.at(i) == '\0'))
  12. {
  13. iSpace ++;
  14. }
  15. }
  16. 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
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 37
Reputation: neosomosis is an unknown quantity at this point 
Solved Threads: 0
neosomosis's Avatar
neosomosis neosomosis is offline Offline
Light Poster

Re: Count characters of a string.

 
0
  #2
Dec 18th, 2007
yup...looks fine to me. How about modifies it for user inputs? See if it can manipulate with users' requirements
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 185
Reputation: eranga262154 is an unknown quantity at this point 
Solved Threads: 2
eranga262154's Avatar
eranga262154 eranga262154 is offline Offline
Junior Poster

Re: Count characters of a string.

 
0
  #3
Dec 18th, 2007
Dealing with the user inputs wont difficult. Anyway I'll check it. What I'm really worried about is the way I find the characters of the string.
“victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 37
Reputation: neosomosis is an unknown quantity at this point 
Solved Threads: 0
neosomosis's Avatar
neosomosis neosomosis is offline Offline
Light Poster

Re: Count characters of a string.

 
0
  #4
Dec 18th, 2007
Easy...Try to insert the characters into an array. That way, you can see what contains in it more clearly
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 185
Reputation: eranga262154 is an unknown quantity at this point 
Solved Threads: 2
eranga262154's Avatar
eranga262154 eranga262154 is offline Offline
Junior Poster

Re: Count characters of a string.

 
0
  #5
Dec 18th, 2007
Use of an array can be more works. What it the my code odd you can see. Please let me know.
“victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: Count characters of a string.

 
0
  #6
Dec 18th, 2007
Use isspace for a check like this.
  1. for(int i = 0; i < iLength; i++)
  2. {
  3. if(!isspace(strTemp.at(i)))
  4. {
  5. iSpace++;
  6. }
  7. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 37
Reputation: neosomosis is an unknown quantity at this point 
Solved Threads: 0
neosomosis's Avatar
neosomosis neosomosis is offline Offline
Light Poster

Re: Count characters of a string.

 
0
  #7
Dec 18th, 2007
I was gonna post it ;(
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 37
Reputation: neosomosis is an unknown quantity at this point 
Solved Threads: 0
neosomosis's Avatar
neosomosis neosomosis is offline Offline
Light Poster

Re: Count characters of a string.

 
0
  #8
Dec 18th, 2007
Correction to WolfPack post....

  1. for(int i = 0; i < iLength; i++){
  2. if(isspace(strTemp.at(i)))
  3. {
  4. iSpace ++;
  5. }
  6. }
Last edited by neosomosis; Dec 18th, 2007 at 5:51 am. Reason: typo
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 185
Reputation: eranga262154 is an unknown quantity at this point 
Solved Threads: 2
eranga262154's Avatar
eranga262154 eranga262154 is offline Offline
Junior Poster

Re: Count characters of a string.

 
0
  #9
Dec 18th, 2007
Seems both codes are same. And how to handle tabs there.
“victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 37
Reputation: neosomosis is an unknown quantity at this point 
Solved Threads: 0
neosomosis's Avatar
neosomosis neosomosis is offline Offline
Light Poster

Re: Count characters of a string.

 
0
  #10
Dec 18th, 2007
if you meant by ignoring the white spaces, you can use a struct @ while(ws != '\t')
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: 13189 | Replies: 18
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC