Thread: whitespace

  1. #16
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    strlen() is slow. It has to start at the beginning of the string and see if each succeeding character is a NULL or not. I only call strlen() once, though.

    isspace() is very fast, and besides you would need to call it too.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  2. #17
    Registered User
    Join Date
    Oct 2005
    Posts
    133
    Hi sly im not sure what you mean in your example if you get time could you put up example code (sorry im a bit slow at things like this)

  3. #18
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    This is what Sly means:
    Code:
    size_t x = 0;
    char other[somesize]'
    while(isspace(s[x++]));
    strcpy(other, &s[x]);
    strcpy(s, other);
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #19
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    why use library functions?

    Code:
    char *
    trim(char * dst, const char * src)
    {
    	char * result = dst;
    	
    	while(*src == ' ')
    	{
    		++src;
    	}
    	
    	while(*src != ' ' && *src != 0)
    	{
    		*dst++ = *src++;
    	}
    	
    	*dst = 0;
    	
    	return result;
    }
    
    char *
    trim(char * str)
    {
    	return trim(str, str);
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #20
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    *claps* I was looking for something similar to that without library functions. I couldn't think of it, and then dinner was ready. Nice one.
    Sent from my iPadŽ

  6. #21
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    why use library functions?
    ...because you have to consider your audience?

    A nice simple solution is reading in the beginning with a regular extraction eliminating the beginning whitespace, reading in the rest with a getline using a space delimeter
    I'm not sure how that is supposed to work. Here is an example sentence that was read into a char array using getline():
    Code:
    "     Hi my name is Kim.    "
    and the op would like to remove the leading and trailing spaces.

    You are advising to read in the first word using operator>>() to get "Hi". Check. That removes the leading whitespace. Then, you are suggesting using getline() with a space as the delimiter to read in the rest of the sentence? Won't that only get you " my"?
    Last edited by 7stud; 02-18-2006 at 07:53 PM.

  7. #22
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    Easiest solution for removing the initial whitespaces would be to declare another char pointer and then iterate through the string till the first non-whitespace character is found and let the the char pointer store the adress for that position. This way the whitespaces aren't actually removed and you won't need to move around the whole array.

    [edit] Oh, and this would mean that you would have to use this new char pointer for whatever task you needed to do instead of your array called "s".
    Last edited by OnionKnight; 02-18-2006 at 07:51 PM.

  8. #23
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    let the the char pointer store the adress for that position.
    ...and then what?

  9. #24
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    I don't know what you mean by "then what" but here's an example of the array "s" after the trailing whitespaces are gone and the position for the first non-whitespace character is found and also pretending that the first character of "s" had the adress of 340 and that it'sthe array has become really small.

    Code:
     340            343
      |              |
      v              v
    [' '][' '][' ']['s']['t']['h']['\0'][' '][' ']
    This would make the new char pointer contain the adress s+3, or 340+3 or 343.
    I don't see any problem with this solution.

  10. #25
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    in that case you'd need to return two pointers: one to the first non-whitespace and another to last non-whitespace character (or even a pointer/length pair).
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  11. #26
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    Relying on the zero termination for the last non-whitespace character shouldn't cause any trouble.

  12. #27
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Yeah for boredom:
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    string trim(const string&);
    
    int main()
    {
      string test = "                 Hello I'm a test string!                  ";
      string trimed = trim(test);
      cout<<"Test: \""<<test<<"\""<<endl;
      cout<<"trimed: \""<<trimed<<"\""<<endl;
      cin.ignore();
    }
    
    string trim(const string& st)
    {
      int first = st.find_first_not_of(" ");
      int last = st.find_last_not_of(" ");
      first = first == string::npos ? 0 : first;
      return st.substr( first, last-first );
    }

  13. #28
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by 7stud
    You are advising to read in the first word using operator>>() to get "Hi". Check. That removes the leading whitespace. Then, you are suggesting using getline() with a space as the delimiter to read in the rest of the sentence? Won't that only get you " my"?
    Yes, you're correct. I hadn't followed up on it because I noticed its problems. Infact, the truth is, if you used a space delimeter, it wouldn't even get to the my and just read nothing.
    Sent from my iPadŽ

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. new to C...need to eliminate whitespace
    By cakestler in forum C Programming
    Replies: 7
    Last Post: 02-02-2009, 12:41 PM
  2. Replies: 10
    Last Post: 06-20-2006, 03:07 PM
  3. Removing Leading & Trailing Whitespace
    By EvilGuru in forum C Programming
    Replies: 11
    Last Post: 12-01-2005, 02:59 PM
  4. Regaurding SetConsoleTitle() and whitespace.
    By Tronic in forum Windows Programming
    Replies: 4
    Last Post: 03-26-2004, 09:02 PM
  5. Whitespace and scanf()
    By Procyon in forum C Programming
    Replies: 1
    Last Post: 01-05-2002, 01:55 AM