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
",h);
    return 0;
}

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

原文地址:https://www.cnblogs.com/fightformylife/p/4361148.html