C实现去空格的实例

//去左空格
char* ltrim(char *ptr)
{
    int start,end,i; 
    end=strlen(ptr)-1;
    if (ptr)  
    {  
        for(start=0; isspace(ptr[start]); start++)  
            ;  
        for(i=start; i<=end; i++)  
            ptr[i-start]=ptr[i];  
        ptr[end-start+1]='\0';  
        return (ptr);  
    }  
    else  
        return NULL; 
}
//去右空格
char* rtrim(char *ptr)
{
    int start,end,i; 
    start=0;
    if (ptr)  
    {  
        for(end=strlen(ptr)-1; isspace(ptr[end]); end--)  
           ;  
        for(i=start; i<=end; i++)  
            ptr[i-start]=ptr[i];  
        ptr[end-start+1]='\0';  
        return (ptr);  
    }  
    else  
        return NULL; 
}

//去两边空格
char * trim(char * ptr)  
{  
    int start,end,i;  
    if (ptr)  
    {  
        for(start=0; isspace(ptr[start]); start++)  
            ;  
        for(end=strlen(ptr)-1; isspace(ptr[end]); end--)  
            ;  
        for(i=start; i<=end; i++)  
            ptr[i-start]=ptr[i];  
        ptr[end-start+1]='\0';  
        return (ptr);  
    }  
    else  
        return NULL;  
}
//去所有空格
char* alltrim(char *dstr)
{
    int i,j = 0;
    char tmp[4096] = {0};
    if (dstr)  
    {
        strcpy(tmp,dstr);

        for (i=0;i<strlen(tmp);i++)
        {
            
            if (!isspace(tmp[i])&&tmp[i]!=NULL)
            {
                dstr[j] = tmp[i];    
                j++;
                
            }
            
        }
        dstr[j] = '\0';
        return (dstr);
    }else{
        return NULL;
    }
}

 

posted @ 2014-08-05 12:08  三道暮光  阅读(422)  评论(0)    收藏  举报