C语言 百炼成钢25

/*
题目61:编写一个名为removestring的函数,该函数用于从一个字符串中删除一定量的字符。
该函数接受三个参数:
第1参数代表源字符串
第2参数代表需要删除字符的起始位置(位置从0开始)
第3参数代表需要删除的字符个数。
eg:字符串"abcd12345efg"
removestring(text, 4, 5);
则会删除该字符数组中的字符串wrong以及后面的空格。遗留内容则是字符串abcdefg"。

*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>


int removestring(char *pstr/*in*/, int begin, int num,char **pout/*out*/){
    int ERRO_MSG = 0;
    if (pstr == NULL || pout==NULL)
    {
        ERRO_MSG = 1;
        printf("pstr == NULL || pout==NULL 传入参数不可以为空  erro msg:%d
", ERRO_MSG);
        return ERRO_MSG;
    }
    //定义临时变量接受参数
    char *pin = pstr;
    //分配返回内存
    char *res = (char *)malloc(sizeof(char)* 30);
    int index = 0,numx=0;
    while (*pin != ''){
        if (index >= begin&&index <(begin+num))
        {
            pin++;
            index++;
            continue;
        }
        res[numx] = *pin;
        //指针后移一位
        pin++;
        index++;
        numx++;
    }
    res[numx] = '';
    *pout = res;
    return ERRO_MSG;
}

int freestring(char **pin/*in*/){
    int ERRO_MSG = 0;
    if (pin==NULL)
    {
        ERRO_MSG = 1;
        printf("pin==NULL 传入参数不可以为空 erro msg:%d
", ERRO_MSG);
        return ERRO_MSG;
    }
    if (*pin!=NULL)
    {
        free(*pin);
        *pin = NULL;
    }
    return ERRO_MSG;
}

void main(){
    char str[30]="abcd12345efg";
    int ret = 0;
    //接收返回字符串
    char *str2 = NULL;
    ret = removestring(str, 0, 5, &str2);
    if (ret!=0)
    {
        printf("删除指定字符失败!");
    }
    //打印处理之后的字符
    printf(str2);
    printf("
");
    //释放内存
    freestring(&str2);
    system("pause");
}

原文地址:https://www.cnblogs.com/zhanggaofeng/p/5657518.html