removing Spaces

Discussion in 'C Programming' started by Magix, Sep 9, 2004.

  1. Magix

    Magix Guest

    Hi,

    Everytime I received a fix-length of string, let say 15 (the unused portion
    will filled with Spaces before receive), I want to remove the Spaces from
    END until I encounter a non-space char.
    let say: testBuf is the buffer that receive this fix-length string.

    printf("[%s]",testBuf);
    Output:
    [Hello World ]

    How to remove the spaces to become [Hello World] ? Any fast and effcient C
    algorithm like using strchr,etc?

    Thanks.
     
    Magix, Sep 9, 2004
    #1
    1. Advertising


  2. > -----Original Message-----
    > From: Magix [mailto:]
    > Posted At: 09 September 2004 10:25
    > Posted To: c
    > Conversation: removing Spaces
    > Subject: removing Spaces
    >
    >
    > Hi,
    >
    > Everytime I received a fix-length of string, let say 15 (the
    > unused portion
    > will filled with Spaces before receive), I want to remove the
    > Spaces from
    > END until I encounter a non-space char.
    > let say: testBuf is the buffer that receive this fix-length string.
    >
    > printf("[%s]",testBuf);
    > Output:
    > [Hello World ]
    >
    > How to remove the spaces to become [Hello World] ? Any fast
    > and effcient C
    > algorithm like using strchr,etc?
    >
    > Thanks.
    >
    >


    This should get you started. But you will need to include some
    error checking though.

    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>

    int main (void)
    {
    char testBuff[]="Hello World! ";

    int len ;

    len = strlen(testBuff)-1;

    while(isspace(testBuff[len]))
    len--;


    testBuff[len]='\0';

    printf("Buffer = [%s]\n",testBuff);

    return 0;
    }

    GST
     
    Turner, GS (Geoff), Sep 9, 2004
    #2
    1. Advertising

  3. Magix

    Magix Guest

    "Magix" <> wrote in message
    news:41402084$...
    > Hi,
    >
    > Everytime I received a fix-length of string, let say 15 (the unused

    portion
    > will filled with Spaces before receive), I want to remove the Spaces from
    > END until I encounter a non-space char.
    > let say: testBuf is the buffer that receive this fix-length string.
    >
    > printf("[%s]",testBuf);
    > Output:
    > [Hello World ]
    >
    > How to remove the spaces to become [Hello World] ? Any fast and effcient C
    > algorithm like using strchr,etc?
    >
    > Thanks.
    >
    >

    I have something like this, but any better way ?

    void str_delete(char * sourc, int pos, int len)
    {
    int i, j;

    --pos;
    j=strlen(sourc);
    i=pos+len;
    while (i <= j)
    {
    sourc[pos]=sourc;
    ++i;
    ++pos;
    }
    }

    int isNonSpaceFound=0;
    for (i=(strlen(testBuf)-1);i>0;i--)
    {
    if (testBuf!=0x20)
    {
    isNonSpaceFound =1;
    }
    if (isNonSpaceFound==0)
    {
    str_delete(testBuf,strlen(testBuf),1);
    }
    }
     
    Magix, Sep 9, 2004
    #3
  4. Magix

    Flash Gordon Guest

    On Thu, 9 Sep 2004 11:40:23 +0100
    "Turner, GS (Geoff) " <> wrote:

    > Return-Receipt-To: "Turner, GS (Geoff) " <>


    Do you really want receipt notification from everyone who reads this
    group?

    <snip>

    > > Everytime I received a fix-length of string, let say 15 (the
    > > unused portion
    > > will filled with Spaces before receive), I want to remove the
    > > Spaces from
    > > END until I encounter a non-space char.
    > > let say: testBuf is the buffer that receive this fix-length string.
    > >
    > > printf("[%s]",testBuf);
    > > Output:
    > > [Hello World ]
    > >
    > > How to remove the spaces to become [Hello World] ? Any fast
    > > and effcient C
    > > algorithm like using strchr,etc?
    > >
    > > Thanks.
    > >
    > >

    >
    > This should get you started. But you will need to include some
    > error checking though.
    >
    > #include <stdio.h>
    > #include <string.h>
    > #include <ctype.h>
    >
    > int main (void)
    > {
    > char testBuff[]="Hello World! ";


    Please don't use tabs to indent on posts to Usenet. There has been a
    discussion about this recently here in comp.lang.c

    > int len ;
    >
    > len = strlen(testBuff)-1;
    >
    > while(isspace(testBuff[len]))


    This doesn't only check for the space character, so it might not be what
    the OP wants.

    > len--;
    >
    >
    > testBuff[len]='\0';


    Here you eradicate the last non-space character.

    > printf("Buffer = [%s]\n",testBuff);
    >
    > return 0;
    > }


    Here is a slightly edited version of your program.

    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>

    int main (void)
    {
    char testBuff[]="Hello World! ";
    int len ;

    len = strlen(testBuff);

    while(isspace(testBuff[--len]))
    /* do nothing */;
    len++;

    testBuff[len]='\0';
    printf("Buffer = [%s]\n",testBuff);

    return 0;
    }
    --
    Flash Gordon
    Sometimes I think shooting would be far too good for some people.
    Although my email address says spam, it is real and I read it.
     
    Flash Gordon, Sep 9, 2004
    #4
  5. Magix

    CBFalconer Guest

    "Turner, GS (Geoff)" wrote:
    >> From: Magix [mailto:]
    >>
    >> Everytime I received a fix-length of string, let say 15 (the
    >> unused portion will filled with Spaces before receive), I want
    >> to remove the Spaces from END until I encounter a non-space
    >> char. let say: testBuf is the buffer that receive this

    << fix-length string.
    >>
    >> printf("[%s]",testBuf);
    >> Output:
    >> [Hello World ]
    >>
    >> How to remove the spaces to become [Hello World] ? Any fast
    >> and effcient C algorithm like using strchr,etc?

    >
    > This should get you started. But you will need to include some
    > error checking though.
    >
    > #include <stdio.h>
    > #include <string.h>
    > #include <ctype.h>
    >
    > int main (void)
    > {
    > char testBuff[]="Hello World! ";
    > int len ;
    >
    > len = strlen(testBuff)-1;
    > ** while(isspace(testBuff[len]))
    > ** len--;
    > ** testBuff[len]='\0';
    > printf("Buffer = [%s]\n",testBuff);
    > return 0;
    > }


    Won't work like that. Replace the lines marked with ** with:

    while (len && (' ' == testBuff[len]))
    testBuff[len--] = '\0';

    --
    "Churchill and Bush can both be considered wartime leaders, just
    as Secretariat and Mr Ed were both horses." - James Rhodes.
    "We have always known that heedless self-interest was bad
    morals. We now know that it is bad economics" - FDR
     
    CBFalconer, Sep 9, 2004
    #5
  6. "Magix" <> wrote in message
    news:41402084$...
    > Hi,
    >
    > Everytime I received a fix-length of string, let say 15 (the unused
    > portion
    > will filled with Spaces before receive), I want to remove the Spaces from
    > END until I encounter a non-space char.
    > let say: testBuf is the buffer that receive this fix-length string.
    >
    > printf("[%s]",testBuf);
    > Output:
    > [Hello World ]
    >
    > How to remove the spaces to become [Hello World] ? Any fast and effcient C
    > algorithm like using strchr,etc?


    You've had a couple of examples, but they both use strlen(). Using strlen
    means you're stepping forward over the string once to find its length, then
    in the examples you've been given, you're stepping backwards over the string
    to find the first non-space character. While this works fine, you can do the
    entire operation by just stepping forward over the string once like this:

    void str_trim (char *s) {
    char *p;
    char tog=0;
    p=s;

    while (*s != '\0') {
    if (*s == ' ' && !tog) {
    tog=1;
    p=s;
    } else if (*s != ' ') {
    tog=0;
    }
    ++s;
    }
    if (tog) {
    *p='\0';
    }
    }

    This may be quicker than the other methods preposed - it may be slower as
    there are more equality tests, but I doubt it makes a great deal of
    difference either way.


    ~Kieran Simkin
    Digital Crocus
    http://digital-crocus.com/
     
    Kieran Simkin, Sep 9, 2004
    #6

  7. > -----Original Message-----
    > From: Flash Gordon [mailto:]
    > Posted At: 09 September 2004 13:03
    > Posted To: c
    > Conversation: removing Spaces
    > Subject: Re: removing Spaces
    >
    >
    > On Thu, 9 Sep 2004 11:40:23 +0100
    > "Turner, GS (Geoff) " <> wrote:
    >
    > > Return-Receipt-To: "Turner, GS (Geoff) " <>

    >
    > Do you really want receipt notification from everyone who reads this
    > group?
    >
    > <snip>
    >

    <snip>
    Have I turned it off now?
     
    Turner, GS (Geoff), Sep 9, 2004
    #7
  8. Magix wrote:

    > "Magix" <> wrote in message
    > news:41402084$...
    >
    >>Hi,
    >>
    >>Everytime I received a fix-length of string, let say 15 (the unused

    >
    > portion
    >
    >>will filled with Spaces before receive), I want to remove the Spaces from
    >>END until I encounter a non-space char.
    >>let say: testBuf is the buffer that receive this fix-length string.
    >>
    >>printf("[%s]",testBuf);
    >>Output:
    >>[Hello World ]
    >>
    >>How to remove the spaces to become [Hello World] ? Any fast and effcient C
    >>algorithm like using strchr,etc?
    >>
    >>Thanks.
    >>
    >>

    >
    > I have something like this, but any better way ?
    >
    > void str_delete(char * sourc, int pos, int len)
    > {
    > int i, j;
    >
    > --pos;
    > j=strlen(sourc);
    > i=pos+len;
    > while (i <= j)
    > {
    > sourc[pos]=sourc;
    > ++i;
    > ++pos;
    > }
    > }
    >
    > int isNonSpaceFound=0;
    > for (i=(strlen(testBuf)-1);i>0;i--)
    > {
    > if (testBuf!=0x20)
    > {
    > isNonSpaceFound =1;
    > }
    > if (isNonSpaceFound==0)
    > {
    > str_delete(testBuf,strlen(testBuf),1);
    > }
    > }
    >
    >

    Yes, there are better methods. So far, I haven't seen
    anything that skips the space between "Hello" and "World"
    and deletes the remaining spaces.

    One idea: Extract the characters between '[' and ']'
    as a string, then trim all the spaces off the right end.

    Here is the second idea:
    void Special_Trim(char * text)
    {
    char * p_rt_bracket = strchr(text, ']');
    if (!p_rt_bracket)
    {
    char * p_non_space = p_rt_bracket;

    /* Point to first character left of ']' */
    --p_non_space;

    /* Point to first non-space character that */
    /* is to the left of the ']'. */
    while ( (p_non_space != text)
    && (*p_non_space == ' '))
    {
    --p_non_space;
    }

    /* Copy from the ']' and beyond over the */
    /* space. */
    ++p_non_space;
    while (*p_rt_bracket)
    {
    *p_non_space++ = *p_rt_bracket++;
    }
    *p_non_space = *p_rt_bracket;
    }
    return;
    }

    --
    Thomas Matthews

    C++ newsgroup welcome message:
    http://www.slack.net/~shiva/welcome.txt
    C++ Faq: http://www.parashift.com/c -faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.learn.c-c++ faq:
    http://www.comeaucomputing.com/learn/faq/
    Other sites:
    http://www.josuttis.com -- C++ STL Library book
     
    Thomas Matthews, Sep 9, 2004
    #8
  9. Magix

    Flash Gordon Guest

    On Thu, 9 Sep 2004 16:15:03 +0100
    "Turner, GS (Geoff) " <> wrote:

    > > From: Flash Gordon [mailto:]
    > >
    > > On Thu, 9 Sep 2004 11:40:23 +0100
    > > "Turner, GS (Geoff) " <> wrote:
    > >
    > > > Return-Receipt-To: "Turner, GS (Geoff) " <>

    > >
    > > Do you really want receipt notification from everyone who reads this
    > > group?
    > >
    > > <snip>
    > >

    > <snip>
    > Have I turned it off now?


    Yes, that's better.
    --
    Flash Gordon
    Sometimes I think shooting would be far too good for some people.
    Although my email address says spam, it is real and I read it.
     
    Flash Gordon, Sep 9, 2004
    #9
  10. Magix

    Joe Wright Guest

    Kieran Simkin wrote:

    > "Magix" <> wrote in message
    > news:41402084$...
    >
    >>Hi,
    >>
    >>Everytime I received a fix-length of string, let say 15 (the unused
    >>portion
    >>will filled with Spaces before receive), I want to remove the Spaces from
    >>END until I encounter a non-space char.
    >>let say: testBuf is the buffer that receive this fix-length string.
    >>
    >>printf("[%s]",testBuf);
    >>Output:
    >>[Hello World ]
    >>
    >>How to remove the spaces to become [Hello World] ? Any fast and effcient C
    >>algorithm like using strchr,etc?

    >
    >
    > You've had a couple of examples, but they both use strlen(). Using strlen
    > means you're stepping forward over the string once to find its length, then
    > in the examples you've been given, you're stepping backwards over the string
    > to find the first non-space character. While this works fine, you can do the
    > entire operation by just stepping forward over the string once like this:
    >
    > void str_trim (char *s) {
    > char *p;
    > char tog=0;
    > p=s;
    >
    > while (*s != '\0') {
    > if (*s == ' ' && !tog) {
    > tog=1;
    > p=s;
    > } else if (*s != ' ') {
    > tog=0;
    > }
    > ++s;
    > }
    > if (tog) {
    > *p='\0';
    > }
    > }
    >
    > This may be quicker than the other methods preposed - it may be slower as
    > there are more equality tests, but I doubt it makes a great deal of
    > difference either way.
    >
    >
    > ~Kieran Simkin
    > Digital Crocus
    > http://digital-crocus.com/
    >
    >


    char * rtrim(char *str) {
    char *s, *p; int c;
    s = p = str;
    while ((c = *s++)) if (c != ' ') p = s;
    *p = '\0';
    return str;
    }

    --
    Joe Wright mailto:
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---
     
    Joe Wright, Sep 10, 2004
    #10
  11. Magix

    CBFalconer Guest

    Joe Wright wrote:
    >

    .... snip ...
    >
    > char * rtrim(char *str) {
    > char *s, *p; int c;
    > s = p = str;
    > while ((c = *s++)) if (c != ' ') p = s;
    > *p = '\0';
    > return str;
    > }


    That has the usage trap (discussed in another thread) of returning
    the same string. Better IMO is this slight modification:

    size_t rtrim(char *s)
    {
    char *s, *p, c;

    s = p = str;
    while ((c = *s++))
    if (' ' != c) p = s;
    *p = '\0';
    return p - str;
    }

    i.e. having done the work to scan over the string, return the
    length which may be useful and can avoid a relatively expensive
    future call to strlen.

    --
    "Churchill and Bush can both be considered wartime leaders, just
    as Secretariat and Mr Ed were both horses." - James Rhodes.
    "We have always known that heedless self-interest was bad
    morals. We now know that it is bad economics" - FDR
     
    CBFalconer, Sep 10, 2004
    #11
  12. Magix

    Paul Guest

    "Magix" <> wrote in message news:<41402084$>...
    > Hi,
    >
    > Everytime I received a fix-length of string, let say 15 (the unused portion
    > will filled with Spaces before receive), I want to remove the Spaces from
    > END until I encounter a non-space char.
    > let say: testBuf is the buffer that receive this fix-length string.
    >
    > printf("[%s]",testBuf);
    > Output:
    > [Hello World ]
    >
    > How to remove the spaces to become [Hello World] ? Any fast and effcient C
    > algorithm like using strchr,etc?
    >
    > Thanks.


    void TrimSpace(char *pStr)
    {
    char* pLoc = pStr;
    while(*pStr)
    if(*pStr++ != ' ')
    pLoc = pStr;
    *pLoc = '\0';
    }

    int main(void)
    {
    char yw[] = "You are welcome! ";
    TrimSpace(yw);
    return 0;
    }

    -Paul.
     
    Paul, Sep 10, 2004
    #12
  13. Magix

    Magix Guest

    "CBFalconer" <> wrote in message
    news:...
    > Joe Wright wrote:
    > >

    > ... snip ...
    > >
    > > char * rtrim(char *str) {
    > > char *s, *p; int c;
    > > s = p = str;
    > > while ((c = *s++)) if (c != ' ') p = s;
    > > *p = '\0';
    > > return str;
    > > }

    >
    > That has the usage trap (discussed in another thread) of returning
    > the same string. Better IMO is this slight modification:
    >
    > size_t rtrim(char *s)
    > {
    > char *s, *p, c;
    >
    > s = p = str;
    > while ((c = *s++))
    > if (' ' != c) p = s;
    > *p = '\0';
    > return p - str;
    > }
    >
    > i.e. having done the work to scan over the string, return the
    > length which may be useful and can avoid a relatively expensive
    > future call to strlen.
    >
    > --
    > "Churchill and Bush can both be considered wartime leaders, just
    > as Secretariat and Mr Ed were both horses." - James Rhodes.
    > "We have always known that heedless self-interest was bad
    > morals. We now know that it is bad economics" - FDR
    >
    >


    Thanks to all for your tips.
     
    Magix, Sep 10, 2004
    #13
  14. Magix

    guggach Guest

    hi
    two ways:
    the barbarian one, set a char pointer at end
    of string, come back until black-char.

    Code:
    --------------------

    char *ppp = other_str+strlen(other_str);
    for(--ppp; ppp-other_str >0 && isspace(*ppp); --ppp); /* end for */
    *ppp = 0;
    printf("|%s|\n",other_str);

    --------------------

    ----
    a civilized one (costs more):
    (assuming we can|must sqeeze spaces to one)

    Code:
    --------------------

    char *ppp, *qqq;
    register int blanc = 0;

    for(ppp = qqq = other_str; *ppp; ++ppp){
    if(isspace(*ppp)){ ++blanc; continue; }
    if(blanc) *qqq++ = ' ';
    *qqq++ = *ppp;
    blanc = 0;
    }
    *qqq = 0;
    printf("|%s|\n",other_str);

    --------------------




    --
    guggach
    ------------------------------------------------------------------------
    Posted via http://www.codecomments.com
    ------------------------------------------------------------------------
     
    guggach, Sep 14, 2004
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John B. Matthews
    Replies:
    4
    Views:
    684
    John B. Matthews
    Sep 12, 2008
  2. Roedy Green
    Replies:
    3
    Views:
    645
  3. Tomasz Chmielewski

    [CSS] how can I show spaces as spaces?

    Tomasz Chmielewski, Sep 5, 2009, in forum: HTML
    Replies:
    21
    Views:
    1,654
    Jan C. Faerber
    Sep 10, 2009
  4. Tomasz Chmielewski

    how can I show spaces as spaces, part 2

    Tomasz Chmielewski, Sep 9, 2009, in forum: HTML
    Replies:
    14
    Views:
    904
    Jan C. Faerber
    Sep 10, 2009
  5. johkar
    Replies:
    2
    Views:
    2,999
    Mayeul
    Dec 10, 2009
Loading...

Share This Page