C语言 百炼成钢26
2016-07-10 17:25
399 查看
/* 题目62: 有一下特征字符串"eerrrrqqAB33333ABa333333ABjsfdsfdsa" 编写一个业务函数, 实现功能1:实现按照子串"AB"分割字符串,把“eerrrrqq”,"33333","a333333","jsfdsfdsa" 把实现结果按照二维数组(第2种内存模型)打包传出。 实现功能2:对二维数组(第二种内存模型),进行排序输出 要求1:请自己编写业务一个接口(函数),并实现功能;70分 要求2:编写测试用例。30分 要求3:自己编写内存释放函数 */ #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> #include<string.h> /* 分析: 使用strstr函数可以很快找出AB的地址,把AB替换成\0 这个前面一段就会成为一个新的字符串 */ //分割字符串 int SpitAndSortStr(char *pin/*in*/, char(*pout)[100]/*out*/,int *rnum){ int ERRO_MSG = 0; if (pin == NULL || pout == NULL || rnum==NULL) { ERRO_MSG = 1; printf("pin == NULL || pout==NULL || rnum==NULL 传入参数不可以为空 erro msg:%d\n", ERRO_MSG); return ERRO_MSG; } char *pstart = pin; int index = 0; char *temp = strstr(pstart, "AB"); while (temp != NULL){ *temp = '\0'; strcpy(pout[index], pstart); index++; //指针向后移动2个字节 pstart = temp + 2; temp=strstr(pstart, "AB"); } strcpy(pout[index], pstart); *rnum = index + 1; return ERRO_MSG; } //字符串排序 int sortstr(char (*pin)[100],int num){ int ERRO_MSG = 0; if (pin==NULL) { ERRO_MSG = 1; printf("传入参数不可以为空 erro msg:%d\n", ERRO_MSG); return ERRO_MSG; } int i = 0,j=0; char temp[100] = {0}; for (int i = 0; i < num; i++) { for (j = i + 1; j < num; j++) { if (strcmp(pin[i],pin[j])>0) { strcpy(temp, pin[i]); strcpy(pin[i], pin[j]); strcpy(pin[j], temp); } } } return ERRO_MSG; } //打印二维数组 void print(char(*pin)[100],int num){ if (pin==NULL) { printf("传入的参数不可以为空!\n"); return; } int i = 0; for (i = 0; i < num; i++) { printf("%s\n", pin[i]); } } void main(){ char str[100] = "eerrrrqqAB33333ABa333333ABjsfdsfdsa"; char arr[5][100] = { 0 }; int ret = 0,num=0; ret = SpitAndSortStr(str, arr,&num); if (ret!=0) { printf("分割字符串程序执行出错!\n"); } //打印字符串 print(arr, num); printf("\n------------排序后---------------------\n"); ret = sortstr(arr, num); if (ret != 0) { printf("排序程序执行出错!\n"); } //打印字符串 print(arr, num); printf("程序执行完毕!\n"); system("pause"); }
相关文章推荐
- C++多态的实现及原理详细解析
- C++多态的实现及原理详细解析
- C++各种继承方式的可见性
- POJ - 1088 滑雪 DP+记忆化搜索
- C语言标准库函数qsort排序的介绍与使用
- C语言实验——单词统计
- c++ 如何监控本机共享文件夹变化(通过监控注册表来实现)
- C语言实验——删除指定字符
- cpp环境【Uva10129】【VIJOS2863】玩弄单词
- C++的类型转换
- C语言实验——大小写转换
- c++ 实现QQ空间,腾讯微博,新浪微博微信,豆瓣等分享功能。
- OC和 C语言中的const
- 洛谷P1316 丢瓶盖
- UVa - 11396 Claw Decomposition 二分图的判定
- 【训练题】强连通分量缩点
- C++类的成员函数存储方式(是否属于类的对象)
- C语言实验——保留整数
- C++创建虚拟机调用JAVA类
- 坦克大战----Ubuntu终端游戏