c实现的trim函数

功能:去掉字符串首尾的空格,换行符等空白。

代码:

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

char *trim(char *str)
{
        char *p = str;
        char *p1;
        if(p)
        {
            p1 = p + strlen(str) - 1;
            while(*p && isspace(*p)) 
                p++;
            while(p1 > p && isspace(*p1)) 
                *p1--=0;
        }
        return p;
}

int main()
{

    char a[]="   asa   ";
    char* h=trim(a);
    printf("%s\n",h);
    return 0;
}

ps:不能直接用char* a="    asd   ";因为这是常量字符串,不能修改。

posted @ 2015-03-23 22:00  雄哼哼  阅读(234)  评论(0编辑  收藏  举报