whitespace

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

Join Date: Jul 2005
Posts: 91
Reputation: Jon182 is an unknown quantity at this point 
Solved Threads: 0
Jon182 Jon182 is offline Offline
Junior Poster in Training

whitespace

 
0
  #1
Feb 18th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,740
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 739
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: whitespace

 
0
  #2
Feb 18th, 2006
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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 91
Reputation: Jon182 is an unknown quantity at this point 
Solved Threads: 0
Jon182 Jon182 is offline Offline
Junior Poster in Training

Re: whitespace

 
0
  #3
Feb 18th, 2006
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?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,740
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 739
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: whitespace

 
0
  #4
Feb 18th, 2006
>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:
  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:
  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 );
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

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



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC