remove white space

This is a discussion on remove white space within the C Programming forums, part of the General Programming Boards category; Hello I have a char str[100] which contains a sequence of characters with a blank space at the end. I ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    2

    remove white space

    Hello

    I have a char str[100] which contains a sequence of characters with a blank space at the end.
    I want to remove the blank space.

    EX:
    str contains: "Pokemons are great "
    I want to change it to
    "Pokemons are great"

    Notice that the string doesn't end with a white space.

    What function can do this?

    Thanks.

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    start with the last char
    while(this is white space) put '\0'
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    26
    write a loop to copy all the bytes above space symbol to the lower index, sample:
    for (i = spaceIndex; i <= strlen(str); i++)
    str[i] = str[i] + 1;
    str[strlen(str) - 2] = 0; //string end symbol

  4. #4
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Don't really get what you mean that it doesn't end with a whitespace since you say it ends with a space.

    Anyhow, the string ends with '\0' always. So to delete a character you can put a '\0' where you want the string to end. Thus to eliminate the space at the end:
    Code:
    str[strlen(str) - 1] = '\0';

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by baccardi View Post
    write a loop to copy all the bytes above space symbol to the lower index, sample:
    for (i = spaceIndex; i <= strlen(str); i++)
    str[i] = str[i] + 1;
    str[strlen(str) - 2] = 0; //string end symbol
    you need no copy to get rid of trailing spaces...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    58
    Code:
    $ cat str.c
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <string.h>
    
    int main ()
    {
     char str[100] = "Pokemons are great ";
     int len;
    
    
     len = (int) strlen(str);
    
     printf("str before [%s], len[%d]\n", str, len);
    
     len--;
     while (isspace(*(str + len) )) {
          len--;
     }
    
     *(str + len + 1) = '\0';
    
     printf("str after [%s], len[%d]\n", str, (int) strlen(str));
    
     exit (0);
    }
    
    $ gcc -Wall str.c
    $ a.out
    str before [Pokemons are great ], len[19]
    str after [Pokemons are great], len[18]

  7. #7
    Registered User
    Join Date
    Mar 2009
    Posts
    2
    thanks

  8. #8
    Complete Beginner
    Join Date
    Feb 2009
    Posts
    312
    I once did that myself. Note that the version above fails for strings consisting only of whitespaces that reside at memory locations with whitespaces just before the start of a string.

    Code:
    /* Remove leading whitespaces */
    char *ltrim(char *const s)
    {
            size_t len;
            char *cur;
    
            if(s && *s) {
                    len = strlen(s);
                    cur = s;
    
                    while(*cur && isspace(*cur))
                            ++cur, --len;
    
                    if(s != cur)
                            memmove(s, cur, len + 1);
            }
    
            return s;
    }
    
    /* Remove trailing whitespaces */
    char *rtrim(char *const s)
    {
            size_t len;
            char *cur;
    
            if(s && *s) {
                    len = strlen(s);
                    cur = s + len - 1;
    
                    while(cur != s && isspace(*cur))
                            --cur, --len;
    
                    cur[isspace(*cur) ? 0 : 1] = '\0';
            }
    
            return s;
    }
    
    /* Remove leading and trailing whitespaces */
    char *trim(char *const s)
    {
            rtrim(s);
            ltrim(s);
    
            return s;
    }
    Greets,
    Philip
    All things begin as source code.
    Source code begins with an empty file.
    -- Tao Te Chip

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,632
    From what I see, rtrim() can be simplified to:
    Code:
    /* Remove trailing whitespaces */
    char *rtrim(char *const s)
    {
            if(s && *s) {
                    char *cur = s + strlen(s) - 1;
    
                    while(cur != s && isspace(*cur))
                            --cur;
    
                    cur[isspace(*cur) ? 0 : 1] = '\0';
            }
    
            return s;
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Complete Beginner
    Join Date
    Feb 2009
    Posts
    312
    Good point! Looks like I did copy and paste from ltrim().

    Thanks,
    Philip
    All things begin as source code.
    Source code begins with an empty file.
    -- Tao Te Chip

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,632
    Quote Originally Posted by Snafuist
    Looks like I did copy and paste from ltrim().
    Heheh, but a thought just crossed my mind: maybe we could use a similiar idea to simplify ltrim(), e.g.,
    Code:
    /* Remove leading whitespaces */
    char *ltrim(char *const s)
    {
            if(s && *s) {
                    char *cur = s;
    
                    while(*cur && isspace(*cur))
                            ++cur;
    
                    if(s != cur)
                            memmove(s, cur, strlen(cur) + 1);
            }
    
            return s;
    }
    There is also the possibility of replacing this:
    Code:
    cur[isspace(*cur) ? 0 : 1] = '\0';
    with:
    Code:
    cur[!isspace(*cur)] = '\0';
    but it is quite possibly more obfuscation than simplification.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Feb 2009
    Posts
    138
    Quote Originally Posted by laserlight View Post
    There is also the possibility of replacing this:
    Code:
    cur[isspace(*cur) ? 0 : 1] = '\0';
    with:
    Code:
    cur[!isspace(*cur)] = '\0';
    but it is quite possibly more obfuscation than simplification.
    that only works if isspace returns 0 or 1, and it doesn't have to because any non-zero value counts as true.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,632
    Quote Originally Posted by Meldreth
    that only works if isspace returns 0 or 1, and it doesn't have to because any non-zero value counts as true.
    No, it works if isspace returns zero or non-zero (which is as defined in the C standard), since the logical negation causes zero to become one and non-zero to become zero.

    One more attempt at squeezing some improvement from Snafuist's examples: I am not sure if this is a simplification, an obfuscation, or both, but it is a very micro-optimisation to replace this:
    Code:
    while(*cur && isspace(*cur))
            ++cur;
    with:
    Code:
    while(isspace(*cur) && *++cur);
    EDIT:
    Oh, and we can replace memmove() with:
    Code:
    char *dest = s;
    while ((*dest++ = *cur++));
    Last edited by laserlight; 03-10-2009 at 04:02 PM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 07-03-2009, 11:20 AM
  2. Reading string with white space in C
    By gep in forum C Programming
    Replies: 2
    Last Post: 05-30-2009, 05:18 PM
  3. Remove space in a string
    By alice in forum C Programming
    Replies: 37
    Last Post: 05-18-2004, 12:27 AM
  4. someone who is good at finding and fixing bugs?
    By elfjuice in forum C++ Programming
    Replies: 8
    Last Post: 06-07-2002, 03:59 PM
  5. White space problems in file input
    By cxs00u in forum C++ Programming
    Replies: 4
    Last Post: 03-20-2002, 10:06 PM

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