944,144 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3202
  • C++ RSS
Feb 18th, 2006
0

whitespace

Expand Post »
Hey guys I was just wondering is there a c++ function that will eliminate white space from the start and end of a character array data obtained with the getline function? Thanks.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Jon182 is offline Offline
91 posts
since Jul 2005
Feb 18th, 2006
0

Re: whitespace

No, there's not a function in either the C or C++ library that will do that. You do it manually. First by finding the end of the string and walking back as long as the current character is whitespace, then replace the last occurance of whitespace with '\0'. Then by finding the first non-whitespace character in the string and shifting everything in the string back. You can do the latter with memmove.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Feb 18th, 2006
0

Re: whitespace

Thanks Narue does it have to be replaced with '\0' or is it just a '0'? and would I have to replace all of the whitespace or just the last one for example:

"k,i,m, 0, , , ,0"

Also would it be easier for me beginner to programming not to use memmove as I have no idea how that function works I had a look on google but I didn't understand the examples I found?
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Jon182 is offline Offline
91 posts
since Jul 2005
Feb 18th, 2006
0

Re: whitespace

>does it have to be replaced with '\0' or is it just a '0'?
'\0' is an escape character with the value of 0 that's used as a string terminator. '0' is not the same thing.

>and would I have to replace all of the whitespace or just the last one
Just the last one. If the string has {'t','e','s','t',' ',' ',' ','\0'} then you only need to replace the first space character after 't' with '\0'. That truncates the string appropriately.

>Also would it be easier for me beginner to programming not to use memmove
The manual solution isn't any simpler:
C++ Syntax (Toggle Plain Text)
  1. for ( i = 0; a[i] != '\0'; i++ ) {
  2. if ( !isspace ( a[i] ) )
  3. break;
  4. }
  5.  
  6. if ( i > 0 ) {
  7. for ( j = 0; a[i] != '\0'; i++, j++ )
  8. a[j] = a[i];
  9. a[j] = '\0';
  10. }
Compare that to memmove:
C++ Syntax (Toggle Plain Text)
  1. for ( i = 0; a[i] != '\0'; i++ ) {
  2. if ( !isspace ( a[i] ) )
  3. break;
  4. }
  5.  
  6. if ( i > 0 )
  7. memmove ( a, a + i, strlen ( a + i ) + 1 );
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

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: Displaying hyphens and backslashes in console window
Next Thread in C++ Forum Timeline: Increasing DOS console window





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


Follow us on Twitter


© 2011 DaniWeb® LLC