字符串作为函数参数传入传出

/***
delSpace.c
***/
#include<stdio.h>
#include<string.h>

int DelSpace(char *str)
{
    int iRet = -1;
    int i ;
    int j ;
    int ncount = 0;
    char *out = str;
    if(NULL == str )
    {
        printf("the point in is NULL\n");
        return iRet;
    }

    i = 0;
    j = strlen(out) - 1;
    while(isspace(out[i]) && out[i] != '\0')
    {
        i++;
    }

    while(isspace(out[j]) && out[j] != '\0')
    {
        j--;
    }

    ncount = j - i + 1;
    strncpy(out,out+i,ncount);  //所有指向的地址空间是不能修改的 
    out[ncount] = '\0';
    iRet = 0;
    return iRet;
}

int main()
{
    int iRet = -1;
    //char *p = "   abcdefg     ";  //分配的地址内存位于常量区 
    char buf[256] = "     abcdefd    ";  //这块内存空间分配在临时区栈区,可以进行修改。buf这个变量名是不允许修改的

    //iRet = DelSpace(p);
    iRet = DelSpace(buf);
    if(0 == iRet)
        printf("DelSpace success p = %s\n",buf);
    else
        printf("DelSpace error p = %s\n",buf);
    return 0;
}

分为两个字符串传入传出

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

int trimSpace(char *in,char *out)
{
    int iRet = -1;
    int i = 0;
    int j = 0;
    int k = 0;
    char *tempIn = in;

    if(in == NULL || out == NULL)
    {
        printf("NULL is error\n");
        return iRet;
    }

    i = 0;
    j = strlen(tempIn) - 1;

    while(isspace(tempIn[i]) && tempIn[i] != '\0')
    {
        i++;
    }
    while(isspace(tempIn[j]) && tempIn[j] != '\0')
    {
        j--;
    }

    while(i <= j)
    {
        out[k++] = tempIn[i++];
    }

    out[k] = '\0';
    iRet = 0;
    return iRet;
}

int main()
{
    int iRet = -1;
    char *p = "  abcf   ";
    char buf[256] = {0};

    iRet = trimSpace(p,buf);
    if(iRet != 0)
    {
        printf("trimSpace error\n");
    }
    else
    {
        printf("trimSpace success\n");
    }

    printf("buf = %s\n",buf);
    return 0;

}
01-15 10:24