首页 > 代码库 > 去掉前后空格,返回一个新字符串,C

去掉前后空格,返回一个新字符串,C

#include<stdio.h>

int trimSpace(char *inbuf, char *outbuf)
{
    int ret = 0;
    if (inbuf == NULL || outbuf == NULL)
    {
        ret = -1;
        return ret;
    }

    char * intail = inbuf;
    while (*intail!=\0)
    {
        intail++;
    }
    intail--;
    while (*intail== )
    {
        *intail = \0;
        intail--;
    }
    while (*inbuf ==  )
    {
        inbuf++;
    }
    while (*inbuf!=\0)
    {
        *outbuf++ = *inbuf++;
    }
    *outbuf = \0;

    return ret;
}

int main()
{
    char buf[] = "      abhf dsiu    qwe                aaaaaaaaaa                        ";
    char out[50];
    int ret;
    ret = trimSpace(buf, out);
    if (ret == 0)
    {
        printf("%s.", out);
    }
    else
    {
        printf("err:%d", ret);
        return ret;
    }

    return 0;
}

 

去掉前后空格,返回一个新字符串,C