943,965 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 7893
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 17th, 2006
0

Using isspace with white spaces

Expand Post »
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.
Similar Threads
Reputation Points: 26
Solved Threads: 0
Junior Poster in Training
matrimforever is offline Offline
67 posts
since Sep 2006
Nov 17th, 2006
0

Re: Using isspace with white spaces

Are you reading the sentence all at once, or word by word?
Moderator
Reputation Points: 572
Solved Threads: 115
Mentally Challenged Mod.
WolfPack is offline Offline
1,559 posts
since Jun 2005
Nov 17th, 2006
0

Re: Using isspace with white spaces

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?
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 26
Solved Threads: 0
Junior Poster in Training
matrimforever is offline Offline
67 posts
since Sep 2006
Nov 17th, 2006
3

Re: Using isspace with white spaces

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.
Team Colleague
Reputation Points: 1135
Solved Threads: 172
Super Senior Demiposter
Rashakil Fol is offline Offline
2,479 posts
since Jun 2005
Nov 17th, 2006
0

Re: Using isspace with white spaces

Not sure how to output using a loop? Can you show me please?
Reputation Points: 26
Solved Threads: 0
Junior Poster in Training
matrimforever is offline Offline
67 posts
since Sep 2006
Nov 17th, 2006
0

Re: Using isspace with white spaces

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
Moderator
Reputation Points: 572
Solved Threads: 115
Mentally Challenged Mod.
WolfPack is offline Offline
1,559 posts
since Jun 2005
Nov 17th, 2006
0

Re: Using isspace with white spaces

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:
C++ Syntax (Toggle Plain Text)
  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. }
Moderator
Reputation Points: 3278
Solved Threads: 894
Posting Sage
WaltP is offline Offline
7,744 posts
since May 2006
Nov 17th, 2006
0

Re: Using isspace with white spaces

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. }
Moderator
Reputation Points: 572
Solved Threads: 115
Mentally Challenged Mod.
WolfPack is offline Offline
1,559 posts
since Jun 2005
Nov 17th, 2006
0

Re: Using isspace with white spaces

If you use C++ strings your life would be much simpler coz then you can do something like:

cpp Syntax (Toggle Plain Text)
  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. }
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 720
Failure as a human
~s.o.s~ is offline Offline
8,872 posts
since Jun 2006
Nov 26th, 2006
0

Re: Using isspace with white spaces

How do I remove extra spaces in between?
Reputation Points: 26
Solved Threads: 0
Junior Poster in Training
matrimforever is offline Offline
67 posts
since Sep 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: function call to determine a palindrome number
Next Thread in C++ Forum Timeline: Using a Database with C





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC