• 字符串函数参数传入传出(去空格)


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

    /***
    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
    ");
            return iRet;
        }
    
        i = 0;
        j = strlen(out) - 1;
        while(isspace(out[i]) && out[i] != '')
        {
            i++;
        }
    
        while(isspace(out[j]) && out[j] != '')
        {
            j--;    
        }
    
        ncount = j - i + 1;
        strncpy(out,out+i,ncount);  //所有指向的地址空间是不能修改的 
        out[ncount] = '';
        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
    ",buf);
        else
            printf("DelSpace error p = %s
    ",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
    ");
            return iRet;
        }
    
        i = 0;
        j = strlen(tempIn) - 1;
    
        while(isspace(tempIn[i]) && tempIn[i] != '')
        {
            i++;
        }
        while(isspace(tempIn[j]) && tempIn[j] != '')
        {
            j--;
        }
    
        while(i <= j)
        {
            out[k++] = tempIn[i++];
        }
    
        out[k] = '';
        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
    ");
        }
        else
        {
            printf("trimSpace success
    ");
        }
    
        printf("buf = %s
    ",buf);
        return 0;
        
    }
  • 相关阅读:
    UVALive 3938 一道被我WA了的线段树
    批量删除Zen Cart 无图片商品
    zencart加大数据表字段长度
    CSS字体中英文名称对照表
    zencart产品批量表上传后SEO三要素状态以及特价时间修改
    Linux 文件系统
    VMware Tools 安装步骤
    IDEA安装阿里规约插件
    IDEA 中无法使用 EL 表达式
    Redis主从复制
  • 原文地址:https://www.cnblogs.com/wanghao-boke/p/11604844.html
Copyright © 2020-2023  润新知