Leading and Trailing Blanks 
Author Message
 Leading and Trailing Blanks

Quote:

>Hello -
>    I am wondering if anyone out there has a program (or knows of one)
>that allows one to strip leading and trailing blanks from a string.
>ex:
>    char test[20];
>    strcpy(test,"  TESTING  ");
>    printf("%s", test);
>will produce an output like
>  TESTING  
>that has blanks at the beginning of "TESTING". I would like to
>have the following result
>TESTING
>that has no leading blanks.
>I would *greatly* appreciate any type of help or hints in working with
>this.
>I'll be looking out here, but e-mails are also appreciated :)
>TIA
>Casey

I like this aproach

char *strip(char *s)
{
        char *start, *end, *p;

        start=NULL;
        end = NULL;
        for(p = s; *p; p++)
        {
                if(!isspace(*p))
                {
                        if(!start)
                                start = p;
                        else
                                end = p;
                }
        }

        /* p now pointes to trailling null */

        if(start)
        {
                if(end)
                {
                        memmove(s,start,(end - start) + 1);
                                        /* +1 for the fence post */
                        s[(end - start) + 1] = '\0';
                        /* add that null */
                }
                else
                {
                        memmove(s,start,(end - start) + 2);
                         /* +2 gets the trailing null */
                }
        }
        return s;

Quote:
}

Andrew Snyder

char disclaimer[] = {'\0'}; /* I'm on cash net */


Tue, 30 Jun 1998 03:00:00 GMT  
 Leading and Trailing Blanks


Quote:
>I like this aproach

>char *strip(char *s)
>{
>        char *start, *end, *p;

>        start=NULL;
>        end = NULL;
>        for(p = s; *p; p++)
>        {
>                if(!isspace(*p))
>                {
>                        if(!start)
>                                start = p;
>                        else
>                                end = p;
>                }
>        }

>        /* p now pointes to trailling null */

start points to the first non-space character or is NULL if there isn't one.

end points to the last non-space character assuming that there is more than 1.
I suspect you wan't to lose the else line there so that end always points to
the last non-space character if there is one.

                         if(!start)
                                 start = p;
                         end = p;

Quote:

>        if(start)

Consider that if (start == s) then you don't need to copy at all (but
possibly null character terminate)

Quote:
>        {
>                if(end)
>                {
>                        memmove(s,start,(end - start) + 1);
>                                        /* +1 for the fence post */
>                        s[(end - start) + 1] = '\0';
>                        /* add that null */
>                }
>                else
>                {
>                        memmove(s,start,(end - start) + 2);

'if (end)' determines that at this point end is a null pointer hence
(end - start) is illegal. However if you make the change I suggested
above then if start is not a null pointer then end isn't either so this
code will never be executed.

Quote:
>                         /* +2 gets the trailing null */
>                }
>        }
>        return s;
>}

You could try the following:

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

char *strip(char *str)

{
    char    *p, *start, *end;

    for (p = str; isspace(*p); p++)
        ;

    start = p;
    end = NULL;

    for (; *p; p++) {
        if (!isspace(*p))
            end = p;
    }

    if (end == NULL)
        str[0] = '\0';
    else if (start == str)
        end[1] = '\0';
    else {
        const size_t len = end-start+1;

        memmove(str, start, len);
        str[len] = '\0';
    }

    return str;

Quote:
}

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


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


Thu, 02 Jul 1998 03:00:00 GMT  
 
 [ 17 post ]  Go to page: [1] [2]

 Relevant Pages 
 

 
Powered by phpBB® Forum Software