Remove whitespaces from char*

This is a discussion on Remove whitespaces from char* within the C Programming forums, part of the General Programming Boards category; Hy, How can I remove whitespaces from a string ? I.e. like trim() does in PHP. I'm in trouble with ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    30

    Remove whitespaces from char*

    Hy,

    How can I remove whitespaces from a string ? I.e. like trim() does in PHP. I'm in trouble with making my own.

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Post your code. Basically, set all the whitespace characters into a string of their own, and then use strstr() to find them. If a subsequent strstr() call returns an adjacent address to the prior saved address, you found a character you can remove.

    If you work on your character array from right to left, it's easier.

    Todd
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You need to make your own (or find one on the "interweb").

    It's probably good practice to learn by making your own. The essential principle is that you need a read and a write position in the string, you then read a character. If it's not a space, write it. Continue this until you are at the end of the string. If you want to do exactly trim, then you will need to:
    1. use "isspace()" to identify whitespace characters in the string.
    2. Identify when you reach the first non-space character and stop removing spaces.
    3. When you reach the end of the string, walk back across any whitespace and terminate the string after the point of non-space. [You can of course "remember the last non-space"].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    A nice do/while loop would do the trick
    Code:
    do 
     {
         if (*string != ' ')   
         {
           *nospaces++ = *string;  /*malloc nospaces */
         i++;
         }
         
     } while(*string++);

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    That will remove ALL spaces, which is not what "trim" does, but yes, that works.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Sorry, read to quick. Just change the logic around...perhaps:

    Code:
    if ((string[0] || string[strlen(string)]) == ' ')
    
      /* then assign your char array as needed */

  7. #7
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Code:
    char* trim(char*s)
    {
        char *end = s + strlen(s)-1;
        while(*s && isspace(*s))
            *s++ = 0;
        while(isspace(*end))
            *end-- = 0;
        return s;
    }
    Nice robust and portable solution, returns the start of the trimmed string. Should not be used on dynamically allocated strings, except if the pointer to the string trimmed start is stored at a different variable.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  8. #8
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by slingerland3g View Post
    Sorry, read to quick. Just change the logic around...perhaps:

    Code:
    if ((string[0] || string[strlen(string)]) == ' ')
    
      /* then assign your char array as needed */
    That only trims spaces, not all whitespace.

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    I have a theory: Programmers are such egomaniacs, you can post a homework question without any proof of effort, and eventually someone will give you the complete answer while trying to show off his expertise.
    My best code is written with the delete key.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Prelude View Post
    I have a theory: Programmers are such egomaniacs, you can post a homework question without any proof of effort, and eventually someone will give you the complete answer while trying to show off his expertise.
    I think your theory is right... Particularly if you post to many enough forums.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Quote Originally Posted by Prelude View Post
    I have a theory: Programmers are such egomaniacs, you can post a homework question without any proof of effort, and eventually someone will give you the complete answer while trying to show off his expertise.

    I second that, from what I have seen in these forums since being a member.

  12. #12
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Quote Originally Posted by Prelude View Post
    I have a theory: Programmers are such egomaniacs, you can post a homework question without any proof of effort, and eventually someone will give you the complete answer while trying to show off his expertise.
    I am sorry but to not be misunderstood, firstly it didn't seem like a homework question, secondly even if it was, then the person doing the assignment might use a black box function to get him started, and thirdly the replies up to then were probably more confusing than a simple clean answer itself.
    I don't recall any assignment that is about trimming strings, and i understand the purpose of the forum. But a familiar tool can always help someone to solve an unfamiliar problem.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  13. #13
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >firstly it didn't seem like a homework question
    "Homework" is generally used here as a catchall term for stuff you should attempt to solve on your own before asking for a solution. More often than not it refers to tasks that any sane programmer would use an existing library to accomplish, yet for some reason the "homeworkee" wants to do manually.

    >the replies up to then were probably more confusing than a simple clean answer itself
    I don't disagree, but you'll also notice that the OP didn't ask for clarification.

    >I don't recall any assignment that is about trimming strings
    I've seen them before. In fact, I've seen a lot of assignments that involve string processing, and trim is one of the more basic ones.
    My best code is written with the delete key.

  14. #14
    Registered User
    Join Date
    Dec 2007
    Posts
    30
    Quote Originally Posted by Prelude View Post
    I have a theory: Programmers are such egomaniacs, you can post a homework question without any proof of effort, and eventually someone will give you the complete answer while trying to show off his expertise.
    I don't study IT at college (though I most probably plan to), so it's not a homework. I'm still in highschool and it's just a hobby of mine.

    And..um..what kind of homework would be to remove whitespaces ? It that what it's like studying IT at school ? Solving this kind of puzzles ?

    P.S. Thanks everyone for your help. My own (not sure actually if it's my own cause I read much of it from another forum) code was something like this:

    Code:
    char *trim_spaces (char *str, int c)
    {
    	if (str != NULL)
    	{
    		char ch = (char)c;
    		char *a, *b;
    		for (a = str; *a == ch; a++); str = a;
    
    		for (b = str; *b != ch; b++) ; *b = '\0';
    		return str;
    	}
    	else return str;
    }
    Best wishes, Des
    Last edited by Dave_Sinkula; 03-10-2008 at 09:54 AM. Reason: Tagged.

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Homework can be all sorts of things, and it's obviously not always easy to determine what is homework or not. By the way, I didn't even suggest it was homework, I just didn't want to give you the code, because writing it yourself would be the best way to learn how to do things with strings.

    Of course, finding the solution on another forum only teaches you better google skills, not programming skills.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Running remove() gets permission denied error...
    By edomingox in forum C Programming
    Replies: 4
    Last Post: 01-11-2009, 12:55 PM
  2. whitespaces
    By happyclown in forum C Programming
    Replies: 2
    Last Post: 01-06-2009, 10:33 PM
  3. randomly remove elements
    By joan in forum C++ Programming
    Replies: 6
    Last Post: 12-06-2006, 01:46 PM
  4. randomly remove elements
    By joan in forum C++ Programming
    Replies: 2
    Last Post: 12-06-2006, 12:22 PM
  5. Remove Duplicate Functions
    By dhartwig1023 in forum C++ Programming
    Replies: 1
    Last Post: 09-12-2002, 02:21 PM

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