whitespace

This is a discussion on whitespace within the C++ Programming forums, part of the General Programming Boards category; Hey guys I was just wondering is there a c++ function that will eliminate white space from the start and ...

  1. #1
    182
    182 is offline
    Registered User
    Join Date
    Oct 2005
    Posts
    133

    whitespace

    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.
    Last edited by 182; 02-18-2006 at 03:43 PM.

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    No....

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    You could write your own functions to do both of those things.

    The eliminate-trailing-whitespace is easier -- just start from the end of the string, work backwards until you find a non-whitespace character, add one, and make that the new end of the string. Something like this:
    Code:
    #include <cctype>  // for isspace()
    #include <cstring>  // for strlen()
    #include <cstdio>  // for BUFSIZ, about 512
    
    char s[BUFSIZ];
    size_t x;  // size_t is an unsigned integer type. It's what strlen() returns
    /* read in s somehow */
    for(x = strlen(s)-1; isspace(s[x]); x --);
    s[x+1] = 0;
    The remove-leading-whitespace is a bit trickier. You'd have to start at the beginning of the string, find the first non-whitespace character, and move the rest of the string, starting with that character, to the beginning.
    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. #4
    182
    182 is offline
    Registered User
    Join Date
    Oct 2005
    Posts
    133
    Thanks for the reply but I am quite new to C++ so I don't think I will be able to do it that way as I don't fully understand the code.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    What part don't you understand? size_t?
    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.

  6. #6
    182
    182 is offline
    Registered User
    Join Date
    Oct 2005
    Posts
    133
    size_t and isspace and to be honest I wouldn't be able to create the code for whitespace at the start of the array.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    Well, you could use intand casts and if(x==' '||x=='\n'||...), but size_t and isspace() are easier.
    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.

  8. #8
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,074
    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, then strcpy() the two.
    Sent from my iPadŽ

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    Try something like this:
    Code:
    size_t x = 0;
    while(isspace(s[x++]));
    memmove(s, s+x, strlen(s)+1-x);
    I think that will work but you'd better test it.
    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.

  10. #10
    182
    182 is offline
    Registered User
    Join Date
    Oct 2005
    Posts
    133
    Hey sly what do u mean by a regular extraction.

  11. #11
    182
    182 is offline
    Registered User
    Join Date
    Oct 2005
    Posts
    133
    Quote Originally Posted by dwks
    Try something like this:
    Code:
    size_t x = 0;
    while(isspace(s[x++]));
    memmove(s, s+x, strlen(s)+1-x);
    I think that will work but you'd better test it.
    Hey thanks what does the memmove do?

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    Sly's using an extra buffer, which is inefficent and unnessesary. memove() is better (it handles overlaps).
    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.

  13. #13
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    memmove() copies n characters from source to desination, handling overlaps*.
    Code:
    memmove(const char *dest, const char *source, size_t n);  // or something like that
    * Overlaps: part of the same string. memcpy() is much like memmove() but isn't guaranteed to work with overlaps.
    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.

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    memmove: http://www.mkssoftware.com/docs/man3/memmove.3.asp

    I was wrong -- the prototye is
    Code:
    #include <string.h> 
    
    void *memmove(void *s1, const void *s2, size_t n);
    I don't know why I thought s1 was const.
    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.

  15. #15
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,074
    Quote Originally Posted by dwks
    Sly's using an extra buffer, which is inefficent and unnessesary. memove() is better (it handles overlaps).
    I'm not the king of efficiency, at my point in my programming knowledge, so I'm not sure, but between your two functions of removing beginning and end whitespace, you have how many calls to isspace() and strlen()? I don't know how long one call takes or one execution of reading from a buffer takes, but I'd be willing to bet that one cin and one cin.getline() gets to be quicker after about (random guess) 10 spaces of whitespace. I don't know. Maybe not. If you know, then please enlighten me on some details.
    Sent from my iPadŽ

Page 1 of 2 12 LastLast
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, 11:41 AM
  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, 01:59 PM
  4. Regaurding SetConsoleTitle() and whitespace.
    By Tronic in forum Windows Programming
    Replies: 4
    Last Post: 03-26-2004, 08:02 PM
  5. Whitespace and scanf()
    By Procyon in forum C Programming
    Replies: 1
    Last Post: 01-05-2002, 12:55 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21