编写一个程序,首先输入一个任意字符串,当输入1时,去掉该字符串最前面的“*”,输入2时,去掉字符串中间的“*”,输入3时,去掉最右端的“*”,输入4时,去掉该字符串中的全部“*”,输入5时,程序退出。(要求:5个子功能全部采用子函数来实现,字符串要求使用指针来进行操作)
3条回答 默认 最新
- CSDN专家-深度学习进阶 2022-03-18 10:50关注
#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; }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 1无用