编写一个程序,首先输入一个任意字符串,当输入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无用
悬赏问题
- ¥15 用stata实现聚类的代码
- ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
- ¥170 如图所示配置eNSP
- ¥20 docker里部署springboot项目,访问不到扬声器
- ¥15 netty整合springboot之后自动重连失效
- ¥15 悬赏!微信开发者工具报错,求帮改
- ¥20 wireshark抓不到vlan
- ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
- ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
- ¥15 stata安慰剂检验作图但是真实值不出现在图上