码迷,mamicode.com
首页 > 其他好文 > 详细

C实现去空格的实例

时间:2014-08-05 13:59:19      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   for   art   ar   div   amp   

//去左空格
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;
    }
}

 

C实现去空格的实例,布布扣,bubuko.com

C实现去空格的实例

标签:style   blog   color   for   art   ar   div   amp   

原文地址:http://www.cnblogs.com/kindom/p/3891990.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!