c++或c语言编程问题

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



#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

参考:

#include "stdio.h"
void func(char * str){
    
    char ch[100]={'\0'};
    int i;
    int cnt=0;
    int flag = 0;
    int len = strlen(str);
    
    for(i=0;i<len;i++){
        if(flag==0 && str[i] !='*'){
            flag=1;
        }
        if(flag==1){
            ch[cnt++] = str[i];        
        }
    }
    puts(ch);
        
}

void main()
{
    char *str;
    gets(str);
    func(str);
    
}

您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632