Using isspace with white spaces

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2006
Posts: 67
Reputation: matrimforever is an unknown quantity at this point 
Solved Threads: 0
matrimforever matrimforever is offline Offline
Junior Poster in Training

Using isspace with white spaces

 
0
  #1
Nov 17th, 2006
How do i use the function isspace to take sentences read into an array and make multiple spaces only one space between words? Use an if statement?

if (isspace(aChar) )
cout << aChar;

else (

Not sure what I need here.
Last edited by matrimforever; Nov 17th, 2006 at 1:03 am.
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: Using isspace with white spaces

 
0
  #2
Nov 17th, 2006
Are you reading the sentence all at once, or word by word?
バルサミコ酢やっぱいらへんで
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 67
Reputation: matrimforever is an unknown quantity at this point 
Solved Threads: 0
matrimforever matrimforever is offline Offline
Junior Poster in Training

Re: Using isspace with white spaces

 
0
  #3
Nov 17th, 2006
im trying to take user input in the form of a sentence putting it in an array and then print out changes: Capitalize the first letter, then the rest of the letters are lower case. If there are multiple spaces, make them only one. Maybe I don't have to use isspace?
  1. const int SIZE = 100;
  2. int main()
  3. {
  4. char sentence[SIZE];
  5. cout << "Please enter a short sentence: " << endl;
  6. cin.getline(sentence, SIZE);
  7.  
  8. for (int i = 1; i < 100; i++)
  9. sentence[i] = tolower(sentence[i]);
  10. for (int i = 0; i < 1; i++)
  11. sentence[i] = toupper(sentence[i]);
  12. //if (isspace(sentence))
  13. cout << sentence;
  14.  
  15. return 0;
  16. system("PAUSE");
  17. }
Last edited by matrimforever; Nov 17th, 2006 at 2:07 am.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,047
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: Using isspace with white spaces

 
3
  #4
Nov 17th, 2006
Output the characters in a loop. Keep track of the previous character you've output. If it was a space, then if the current character is a space, don't output it.
All my posts may be redistributed under the GNU Free Documentation License.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 67
Reputation: matrimforever is an unknown quantity at this point 
Solved Threads: 0
matrimforever matrimforever is offline Offline
Junior Poster in Training

Re: Using isspace with white spaces

 
0
  #5
Nov 17th, 2006
Not sure how to output using a loop? Can you show me please?
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: Using isspace with white spaces

 
0
  #6
Nov 17th, 2006
Originally Posted by matrimforever View Post
Maybe I don't have to use isspace?
Yeah. You dont. Consider the use of std::string and std:stringstream.

Anyway this should do for now.

  1. const int SIZE = 100;
  2. int main()
  3. {
  4. /*
  5.   * Remember to initialize char arrays
  6.   */
  7. char sentence[SIZE] = "";
  8. std::cout << "Please enter a short sentence: " << std::endl;
  9. std::cin.getline(sentence, SIZE);
  10.  
  11. /*
  12.   * Remove leading spaces
  13.   */
  14. int i = 0;
  15. while( i < SIZE && isspace( sentence[i] ))
  16. {
  17. i++;
  18. };
  19. /*
  20.   * Check if the array has finished traversing
  21.   */
  22. if ( i == SIZE )
  23. return 0;
  24. /*
  25.   * Capitalize the first non-space character
  26.   */
  27. std::cout << (char)toupper(sentence[i++]);
  28. /*
  29.   * Start after the leading spaces
  30.   * Note that this will leave a trailing space,
  31.   * but that wont be seen.
  32.   */
  33. for ( ; i < SIZE; )
  34. {
  35. std::cout << (char)tolower(sentence[i]);
  36. if (isspace( sentence[i] ))
  37. {
  38. do
  39. {
  40. i++;
  41. }
  42. while( i < SIZE && isspace( sentence[i] ) );
  43. }
  44. else
  45. {
  46. i++;
  47. }
  48. }
  49. return 0;
  50. }
Last edited by WolfPack; Nov 17th, 2006 at 3:28 am. Reason: Added bounds checking, changed do-while to for
バルサミコ酢やっぱいらへんで
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
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: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Using isspace with white spaces

 
0
  #7
Nov 17th, 2006
Originally Posted by matrimforever View Post
Not sure how to output using a loop? Can you show me please?
Simple. Using your code, and formatting it correctly with proper indentation -- please learn this. As your programs grow, they will become unreadable without proper formatting:
  1. const int SIZE = 100;
  2. int main()
  3. {
  4. char sentence[SIZE];
  5. cout << "Please enter a short sentence: " << endl;
  6. cin.getline(sentence, SIZE);
  7.  
  8. for (int i = 1; i < 100; i++)
  9. {
  10. // sentence[i] = tolower(sentence[i]);
  11. cout << tolower(sentence[i]); // output
  12. }
  13. for (int i = 0; i < 1; i++)
  14. {
  15. // sentence[i] = toupper(sentence[i]);
  16. cout << toupper(sentence[i]); // output
  17. }
  18. // return 0; // wrong place
  19.  
  20. // system("PAUSE"); // Yech!!!! Don't use this
  21. getchar(); // instead...
  22.  
  23. return 0; // right place
  24. }
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: 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: Using isspace with white spaces

 
0
  #8
Nov 17th, 2006
Here is one that removes the trailing spaces too.

  1. const int SIZE = 100;
  2. int main()
  3. {
  4. /*
  5.   * Remember to initialize char arrays
  6.   */
  7. char sentence[SIZE] = "";
  8. std::cout << "Please enter a short sentence: " << std::endl;
  9. std::cin.getline(sentence, SIZE);
  10. int len = strlen( sentence );
  11. /*
  12.   * Remove trailing spaces
  13.   */
  14. int i = len -1;
  15. while ( i >= 0 && isspace( sentence[i] ) )
  16. {
  17. i--;
  18. }
  19. /*
  20.   * Check if the array has finished traversing
  21.   */
  22. if ( i < 0 )
  23. {
  24. return 0;
  25. }
  26. /*
  27.   * Terminate the sentence before the trailing spaces and
  28.   * update the new length
  29.   */
  30. sentence[ i + 1 ] = 0;
  31. len = strlen( sentence );
  32. /*
  33.   * Remove leading spaces
  34.   */
  35. i = 0;
  36. while( i < len && isspace( sentence[i] ))
  37. {
  38. i++;
  39. };
  40. /*
  41.   * Check if the array has finished traversing
  42.   */
  43. if ( i == len )
  44. return 0;
  45. /*
  46.   * Capitalize the first non-space character
  47.   */
  48. std::cout << (char)toupper(sentence[i++]);
  49. /*
  50.   * Start after the leading spaces
  51.   */
  52. for ( ; i < len; )
  53. {
  54. std::cout << (char)tolower(sentence[i]);
  55. if (isspace( sentence[i] ))
  56. {
  57. do
  58. {
  59. i++;
  60. }
  61. while( i < len && isspace( sentence[i] ) );
  62. }
  63. else
  64. {
  65. i++;
  66. }
  67. }
  68. return 0;
  69. }
バルサミコ酢やっぱいらへんで
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,620
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 468
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Using isspace with white spaces

 
0
  #9
Nov 17th, 2006
If you use C++ strings your life would be much simpler coz then you can do something like:

  1. #include <string>
  2.  
  3. const std::string whiteSpaces( " \f\n\r\t\v" );
  4.  
  5. void trimRight( std::string& str,
  6. const std::string& trimChars = whiteSpaces )
  7. {
  8. std::string::size_type pos = str.find_last_not_of( trimChars );
  9. str.erase( pos + 1 );
  10.  
  11. }
  12.  
  13.  
  14. void trimLeft( std::string& str,
  15. const std::string& trimChars = whiteSpaces )
  16. {
  17. std::string::size_type pos = str.find_first_not_of( trimChars );
  18. str.erase( 0, pos );
  19. }
  20.  
  21.  
  22. void trim( std::string& str, const std::string& trimChars = whiteSpaces )
  23. {
  24. trimRight( str, trimChars );
  25. trimLeft( str, trimChars );
  26. }
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 67
Reputation: matrimforever is an unknown quantity at this point 
Solved Threads: 0
matrimforever matrimforever is offline Offline
Junior Poster in Training

Re: Using isspace with white spaces

 
0
  #10
Nov 26th, 2006
How do I remove extra spaces in between?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC