码迷,mamicode.com
首页 > 编程语言 > 详细

C语言 百炼成钢24

时间:2016-06-19 15:33:34      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:

/*
题目60:从键盘中输入一个不超过40个字符的字符串,
再输入3个位数(每次删除一个字符),删除对应 位数的字符,然后输出删除指定字符后的字符串。
输入:hellokityManGood
3  6   9
helokityManGood

heloktyManGood

heloktyMnGood

输出:heloktyMnGood
要求1:编写业务接口并实现核心功能 70分
要求2:写出测试用例 30分
*/

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

//思路:在原来字符串上进行删除,需要将后面的所有字符都向前移动一位,感觉比较麻烦
//决定每次只删除一个字符 可以把这个字符以一个特殊代替,重新遍历字符,把除了这个字符外的字符写入新字符串

//删除指定字符
int RemoveStr(char *pin/*in*/,int index){
    int ERRO_MSG = 0;
    if (pin==NULL)
    {
        ERRO_MSG = 1;
        printf("pin==NULL erro msg:%d\n", ERRO_MSG);
        return ERRO_MSG;
    }
    int i = 0,k=0;
    int numx = (int)strlen(pin);
    char buf[40] = { 0 };
    for (i = 0; i < numx; i++)
    {
        if (index!=(i+1))
        {
            buf[k++] = *(pin+i);//注意buf是按条件自增
        }
    }
    //清空原始字符串
    memset(pin, 0, sizeof(char)* 40);
    //拷贝新的字符串
    strcpy(pin, buf);
    printf("新的字符串是%s\n", pin);
    return ERRO_MSG;
}

void main(){
    char buf[40] = { 0 };
    int num = 0;
    printf("请输入不超过40个字符的字符串!\n");
    scanf("%s", buf);
    while (1){
        printf("请输入一个数字\n");
        scanf("%d",&num);
        RemoveStr(buf, num);
    }
    system("pause");
}

 

技术分享

C语言 百炼成钢24

标签:

原文地址:http://www.cnblogs.com/zhanggaofeng/p/5598034.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!