sasigeinaruto 2022-03-18 10:40 采纳率: 75%
浏览 152
已结题

c++或c语言编程问题

编写一个程序,首先输入一个任意字符串,当输入1时,去掉该字符串最前面的“*”,输入2时,去掉字符串中间的“*”,输入3时,去掉最右端的“*”,输入4时,去掉该字符串中的全部“*”,输入5时,程序退出。(要求:5个子功能全部采用子函数来实现,字符串要求使用指针来进行操作)

  • 写回答

3条回答 默认 最新

  • 关注
    
    
    #include <stdio.h>
    #include <stdlib.h>
    
    
    void func1(char *input)
    {
        while(*input == '*')
        {
            input++;
        }
        printf("%s\n", input);
    }
    
    void func2(char *input)
    {
        char output[100];
        char *temp = output;
        char *temp2;
    
        while (*input == '*')
        {
            *temp++ = *input++;
        }
    
        temp2 = input;
        while(*temp2 != '\0')
        {
            temp2++;
        }
    
        temp2--;
        while (*temp2 == '*')
        {
            temp2--;
        }
    
        while (input != temp2)
        {
            if (*input != '*')
                *temp++ = *input++;
            else input++;
        }
    
        while (*input != '\0')
        {
            *temp++ = *input++;
        }
        *temp = '\0';
        printf("%s\n", output);
    }
    
    void func3(char *input)
    {
        char *temp = input;
        while(*temp != '\0')
        {
            temp++;
        }
    
        while (temp != input)
        {
            temp--;
            if (*temp != '*')
            {
                *(temp + 1) = '\0';
                break;
            }
        }
        printf("%s\n", input);
    }
    
    void func4(char *input)
    {
        char output[100];
        char *temp = output;
        while(*input != '\0')
        {
            if (*input != '*')
                *temp++ = *input++;
            else
                input++;
        }
        *temp = '\0';
        printf("%s\n", output);
    }
    
    int main()
    {
        char input[100] = "****aaaa****bbb*cc**d*****";
        printf("输入字符串");
        scanf("%s", input);
        printf("输入1-5\n");
        int x;
        scanf("%d",&x);
        switch(x){
            case 1:func1(input);break;
            case 2:func2(input);break;
            case 3:func3(input);break;
            case 4:func4(input);break;
            case 5:break;
    default:break;
        }
    
        return 0;
    }
    
    

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 3月26日
  • 已采纳回答 3月18日
  • 创建了问题 3月18日

悬赏问题

  • ¥30 Matlab打开默认名称带有/的光谱数据
  • ¥50 easyExcel模板 动态单元格合并列
  • ¥15 res.rows如何取值使用
  • ¥15 在odoo17开发环境中,怎么实现库存管理系统,或独立模块设计与AGV小车对接?开发方面应如何设计和开发?请详细解释MES或WMS在与AGV小车对接时需完成的设计和开发
  • ¥15 CSP算法实现EEG特征提取,哪一步错了?
  • ¥15 游戏盾如何溯源服务器真实ip?需要30个字。后面的字是凑数的
  • ¥15 vue3前端取消收藏的不会引用collectId
  • ¥15 delphi7 HMAC_SHA256方式加密
  • ¥15 关于#qt#的问题:我想实现qcustomplot完成坐标轴
  • ¥15 下列c语言代码为何输出了多余的空格