目录

方案一

方案二

方案三

方案四

五方案


方案一

#define  _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>/*
有一个字符串符合以下特征(“abcdef,acccd,eeee,aaaa,e3eeee,ssss,”)
写两个函数(API),输出以下结果
第一个API
1)以逗号分隔字符串,形成二维数组,并把结果传出
2)把二维数组行数运算结果也传出
第二个API
1)以逗号分隔字符串,形成一个二级指针。
2)把一共拆分多少行字符串个数传出
要求:
1, 能正确表达功能的要求,定义出接口。
2, 正确实现接口和功能.
3, 编写正确的测试用例.
*/int spitString(char *str, char ch, char array[][30], int *count)
{char *p = str;char *q = p;int temp_count = 0;int len = 0;if (str == NULL || array == NULL || count == NULL) {fprintf(stderr, "str == NULL || array == NULL || count == NULL\n");return -1;}//在一个字符串中 找到一个字符  找到了 返回第一个字符的地址, 失败返回NULL//strchr(母串, 字符)while ((p = strchr(p, ch)) != NULL) {//找到了strncpy(array[temp_count], q, p - q);array[temp_count][p - q] = '\0';temp_count++;p++;q = p;if (*p == '\0') {break;}}if (*q != '\0') {len = (str + strlen(str)) - q;strncpy(array[temp_count], q, len);array[temp_count][len] = '\0';temp_count++;}*count = temp_count;return 0;
}int main(void)
{char *str = "abcdef,acccd,eeee,aaaa,e3eeee,ssss,";char array[10][30];int count = 0;int retn = 0;int i = 0;retn = spitString(str, ',', array, &count);if (retn < 0) {fprintf(stderr, "spitString er\n");return -1;}for (i = 0; i < count; i++) {printf("array[%d]:%s\n", i, array[i]);}return 0;
}

方案二

#define  _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>void free_mem(char ***array_p, int count)
{char **array = *array_p;int i = 0;if (array_p == NULL) {return;}void free(void*);if (array != NULL) {for (i = 0; i < count; i++) {if (array[i] != NULL) {free(array[i]);array[i] = NULL;}}free(array);*array_p = NULL;}
}int spitString(char *str, char ch, char ***array_p, int *count)
{char * p = str;char * q = p;int temp_count = 0;char **array = NULL;int str_len = 0;int retn = 0;if (str == NULL || array_p == NULL || count == NULL) {fprintf(stderr, " (str == NULL || array_p == NULL || count == NULL)\n");return -1;}//1 求出 字符串中 拆分的个数while ((p = strchr(p, ch)) != NULL) {temp_count++;p++;q = p;if (*q == '\0') {break;}}if (*q != '\0') {temp_count++;}//此时temp_count 就是 子字符串的个数//2 根据个数开辟指针数组 在堆上array = (char**)malloc(sizeof(char*)* temp_count);if (array == NULL) {fprintf(stderr, "malloc  char **array error\n");retn = -1;goto END;}memset(array, 0, sizeof(char*)*temp_count);//3 拆分字符串, 为每一个指针开辟堆空间 拷贝字符串p = str;q = p;temp_count = 0;while ((p = strchr(p, ch)) != NULL) {//找到了str_len = p - q;array[temp_count] = (char*)malloc(sizeof(char)* (str_len+1));if (array[temp_count] == NULL) {fprintf(stderr, "malloc array[%d] error\n", temp_count);retn = -1;goto END;}strncpy(array[temp_count], q, str_len);array[temp_count][str_len] = '\0';temp_count++;p++;q = p;if (*p == '\0') {break;}}if (*q != '\0') {str_len = (str + strlen(str)) - q;array[temp_count] = (char*)malloc(sizeof(char)*(str_len + 1));if (array[temp_count] == NULL) {fprintf(stderr, "malloc array[%d] error\n", temp_count);retn = -1;goto END;}strncpy(array[temp_count], q, str_len);array[temp_count][str_len] = '\0';temp_count++;}if (array != NULL) {*array_p = array;*count = temp_count;}//释放内存的步骤
END:if (retn != 0) {//已经出现错误了free_mem(&array, temp_count);}return 0;
}int main(void)
{char *str = "abcdef,acccd,eeee,aaaa,e3eeee,ssss,";char **array = NULL;int count = 0;int retn = 0;int i = 0;retn = spitString(str, ',', &array, &count);for (i = 0; i < count; i++) {printf("array[%d]: %s\n", i, array[i]);}free_mem(&array, count);if (array == NULL) {printf("array kong\n");}return 0;
}

方案三

#define  _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>char** mySpitString( char* buf1, char c, int* count)
{char* p = NULL;char* pTmp = NULL;int tmpcount = 0;char** myp = NULL;p = buf1;pTmp = buf1;// 第一次求出countdo {p = strchr(p, c);if (p != NULL){if ( p - pTmp > 0 ){tmpcount++;pTmp = p = p + 1;}}else{break;}} while (*p != '\0');*count = tmpcount;//根据多少行精确的分配 内存myp = (char**)malloc(tmpcount*sizeof(char*));if (myp == NULL){return NULL;}tmpcount = 0;p = buf1;pTmp = buf1;do{p = strchr(p, c);if (p != NULL){//检索符合条件的位置p后移, 形成差值,挖字符串if (p - pTmp > 0){int len = p - pTmp +1;//分配‘\0’的空间myp[tmpcount] = (char*)malloc(len*sizeof(char));if (myp[tmpcount]== NULL) {return NULL;}strncpy(myp[tmpcount], pTmp, p - pTmp);myp[tmpcount][p - pTmp] = '\0'; //把一行数据变成c风格的字符串//让p  ptmp达到下一次检索条件pTmp = p = p + 1;tmpcount++;}}else{break;}} while (*p != '\0');*count = tmpcount;return myp;
}int main(void)
{char *str = "abcdef,acccd,eeee,aaaa,e3eeee,ssss,";int ret = 0, k = 0;char cTem = ',';int nCount = 0;char** p = NULL;p = mySpitString(str, cTem, &nCount);if (p == NULL){printf("error:%d\n", ret);return ret;}for (size_t i = 0; i < nCount; i++){printf("%s\n", p[i]);}//free roomfor (size_t i = 0; i < nCount; i++){free(p[i]);}free(p);system("pause");return 0;}

方案四

#define  _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>char** mySpitString( char* buf1, char c, int* count)
{char* p = NULL;char* pTmp = NULL;int tmpcount = 0;char** myp = NULL;p = buf1;pTmp = buf1;// 第一次求出countdo {p = strchr(p, c);if (p != NULL){if ( p - pTmp > 0 ){tmpcount++;pTmp = p = p + 1;}}else{break;}} while (*p != '\0');*count = tmpcount;//根据多少行精确的分配 内存myp = (char**)malloc(tmpcount*sizeof(char*));if (myp == NULL){return NULL;}tmpcount = 0;p = buf1;pTmp = buf1;do{p = strchr(p, c);if (p != NULL){//检索符合条件的位置p后移, 形成差值,挖字符串if (p - pTmp > 0){int len = p - pTmp +1;//分配‘\0’的空间myp[tmpcount] = (char*)malloc(len*sizeof(char));if (myp[tmpcount]== NULL) {return NULL;}strncpy(myp[tmpcount], pTmp, p - pTmp);myp[tmpcount][p - pTmp] = '\0'; //把一行数据变成c风格的字符串//让p  ptmp达到下一次检索条件pTmp = p = p + 1;tmpcount++;}}else{break;}} while (*p != '\0');*count = tmpcount;return myp;
}
void FreeRoomP(char*** p, int count)
{int i = 0;char** myp = NULL;if (p == NULL){return;}myp = *p;if(myp == NULL){return;}for (; i < count; i++){if (myp[i] != NULL){free(myp[i]);}}if (myp != NULL){free(myp);}*p = NULL; //把实参二级指针,修改为NULL
}void FreeRoom(char** myp, int count)
{int i = 0;if (myp == NULL){return ;}for (; i < count; i++){if (myp[i] != NULL){free(myp[i]);}}if (myp != NULL){free(myp);}}char mySpitStr(char* buf1, char c, char*** myp3, int* count)
{char* p = NULL;char* pTmp = NULL;int tmpcount = 0;char** myp = NULL;int ret = 0;p = buf1;pTmp = buf1;// 第一次求出countdo{p = strchr(p, c);if (p != NULL){if (p - pTmp > 0){tmpcount++;pTmp = p = p + 1;}}else{break;}} while (*p != '\0');*count = tmpcount;//根据多少行精确的分配 内存myp = (char**)malloc(tmpcount*sizeof(char*));if (myp == NULL){//如果这儿出错已分配 的内存要释放 ret = -2;printf("here error myp**malloc(tmpcount*sizeof(char*))\n");goto END;//return -2;}memset(myp, 0, tmpcount*sizeof(char*));tmpcount = 0;p = buf1;pTmp = buf1;do{p = strchr(p, c);if (p != NULL){//检索符合条件的位置p后移, 形成差值,挖字符串if (p - pTmp > 0){int len = p - pTmp + 1;//分配‘\0’的空间myp[tmpcount] = (char*)malloc(len*sizeof(char));if (myp[tmpcount] == NULL){//如果这儿出错已分配 的内存要释放 ret = -2;printf("here error  (char*)malloc(len*sizeof(char)\n");goto END;//return -1;}strncpy(myp[tmpcount], pTmp, p - pTmp);myp[tmpcount][p - pTmp] = '\0'; //把一行数据变成c风格的字符串//让p  ptmp达到下一次检索条件pTmp = p = p + 1;tmpcount++;}}else{break;}} while (*p != '\0');END:if (ret !=0){//free room, 只要一个就可以了,其它可以注释了//free1FreeRoom(myp, *count);//free2FreeRoomP(&myp, *count);//free3int i = 0;if (myp == NULL){return -3;}for (; i < tmpcount; i++){if (myp[i] != NULL){free(myp[i]);}}if (myp != NULL){free(myp);}}else{*myp3 = myp; //suceess*count = tmpcount;}return ret;
}int main(void)
{char *str = "abcdef,acccd,eeee,aaaa,e3eeee,ssss,";int ret = 0, k = 0;char cTem = ',';int nCount = 0;char** p = NULL;//p = mySpitString(str, cTem, &nCount);//if (p == NULL)//{//  printf("error:%d\n", ret);//  return ret;//}//ormySpitStr(str, cTem, &p, &nCount);//采用三级指针for (size_t i = 0; i < nCount; i++){printf("%s\n", p[i]);}//free roomfor (size_t i = 0; i < nCount; i++){free(p[i]);}free(p);system("pause");return 0;}

五方案

#define  _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>#define IN
#define OUTvoid free_mem(char ***array_p, int num) // char ****array = &p
{char **array = *array_p;int i = 0;if (array_p == NULL) {return;}if (array == NULL) {return;}for (i = 0; i < num; i++) {if (array[i] != NULL) {free(array[i]);array[i] = NULL;}}free(array);*array_p = NULL;
}int sort(IN char *array_1[], int num1,IN char(*array_2)[30], int num2,OUT char ***array_3_p, OUT int *num3_p)
{char **p= NULL;int num3 = 0;int retn = 0;int i = 0;int j = 0;int len = 0;char *temp_p = NULL;if (array_1 == NULL || array_2 == NULL || array_3_p == NULL || num3_p == NULL) {fprintf(stderr, "array_1 == NULL || array_2 == NULL || array_3_p == NULL || num3 == NULL\n");return -1;}num3 = num1 + num2; //这个是p3指针所需要第一次开辟的 指针的个数p = (char**)malloc(sizeof(char*)*num3);if (p == NULL) {fprintf(stderr, "malloc char**p3 error\n");retn = -1;goto END;}memset(p, 0, sizeof(char*)*num3);//array_1 copy p中for (i = 0; i < num1; i++) {len = strlen(array_1[i]);p[i] = (char*)malloc(sizeof(char)* (len + 1));memset(p[i], 0, sizeof(char)*(len + 1));strcpy(p[i], array_1[i]);}//array_2 copy p中for (j = 0; j < num2; j++, i++) {len = strlen(array_2[j]);p[i] = (char*)malloc(sizeof(char)*(len + 1));memset(p[i], 0, sizeof(char)*(len + 1));strcpy(p[i], array_2[j]);}for (i = 0; i < num3; i++) {printf("p[%d]:%s\n", i, p[i]);}//对 p 排序for (i = 0; i < num3; i++) {for (j = i; j < num3; j++) {if (strcmp(p[i], p[j]) > 0) {temp_p = p[i];p[i] = p[j];p[j] = temp_p;}}}printf("----\n");for (i = 0; i < num3; i++) {printf("p[%d]:%s\n", i, p[i]);}*array_3_p = p;*num3_p = num3;END:if (retn != 0) {free_mem(&p, num3);}return 0;
}int main(void)
{int ret = 0;char *p1[] = { "aa", "cccccc", "bbbbb" };char buf2[10][30] = { "111111", "3333", "2222" };char **p3 = NULL;int len1, len2,len3, i = 0;len1 = sizeof(p1) / sizeof(*p1);len2 = 3;sort(p1, len1, buf2, len2, &p3, &len3);free_mem(&p3, len3);if (p3 == NULL) {printf("free p3 succ\n");}return 0;
}

字符串分隔为二维数组,二级和三级指针的应用案例相关推荐

  1. 【字符串】面试题之以逗号分割字符串,形成二维数组

    题目: 有一个字符串符合以下特征("abcdef,acccd,eeee,aaaa,e3eeeee,sssss,"), 要求写一个函数(接口),输出以下结果 1) 以逗号分割字符串, ...

  2. matlab 二维数组声明,Matlab字符串函数及二维数组

    Matlab字符串函数及二维数组 发布时间:2017年07月28日 评论数:抢沙发 阅读数:833 strcmp(Str1,Str2),finder(S,s),strcat(S1,S2),disp(s ...

  3. 【C 语言】二级指针案例 ( 字符串切割 | 返回 二维数组 作为结果 )

    文章目录 一.二级指针案例 ( 返回二维数组 ) 二.完整代码示例 一.二级指针案例 ( 返回二维数组 ) 将 "12,ab,345," 字符串 以 逗号 "," ...

  4. 【C 语言】二级指针作为输入 ( 二维数组 | 二维数组内存大小计算 | 指针跳转步长问题 )

    文章目录 一.二维数组内存大小计算 二.二维数组内存大小意义 一.二维数组内存大小计算 给定一个二维数组 : 该 二维数组 中有 444 个 一维数组 , 每个一维数组有 101010 个 char ...

  5. C++获取指向二维数组的首元素指针

    假设一个二维数组int a[][4] = { { 3, 2, 8, 9 }, { 2, 4, 9, 12 }, { 4, 7, 10, 13 }, { 6, 8, 11, 15 } };,获取指向其首 ...

  6. 二维数组解引用解释——指针

    #include <stdio.h>int main() {int array[3][4] = {{0,1,2,3},{4,5,6,7},{8,9,10,11}};int (*p)[4] ...

  7. 二维数组求平均值(指针的使用)

    #include<stdio.h>int main() {int buf[3][5] ={{1,2,3,4,5},{4,5,6,7,8},{7,8,9,10,11}};int i;int ...

  8. C/C++ 一维数组的传参/一级指针的传参 二维数组的传参/二级指针的传参 三维数组的传参/三级指针的传参 方法总结分析终极篇

    序 最近复习c/c++数组的传参,发现了一些问题,下面是一些总结和思考 正文 一维数组的传参/一级指针的传参/普通指针 在理解指针的基础上,一维数组的指针传递很简单,我们知道数组的数组名就是这个数组首 ...

  9. c语言字符串二维数组的动态分配应,C语言中动态分配二维数组复习过程.doc

    C语言中动态分配二维数组复习过程.doc C语言中动态分配二维数组在C中动态分配内存的,对于单个变量,字符串,一维数组等,都是很容易的.C中动态分配二维数组的方法,很少有C语言书中描述,我查找了有的C ...

  10. c语言数组中逗号的作用,c语言练习(4)--逗号分割字符串形成二维数组

    /** 作者:一叶扁舟 时间:23:11 2017/6/4 作用: 有一个字符串符合以下特征("abcdef,acccd,eeee,aaaa,e3eeeee,sssss";),要求 ...

最新文章

  1. android 编译 oserror,Android-4.4.2 编译出错 OSError: [Errno 2] No such file or directory
  2. ka电器表示什么意思_电器上的KA是指的什么电流?
  3. java8编译_为什么在Java7中编译而在Java8中编译?
  4. Vue组件中使用Sass或者Less全局变量
  5. pyquery获取不到网页完整源代码_PyQuery 详解
  6. mysql中实现over partiton by,进行分组排序取topN
  7. 某著名公司2015暑期实习招聘试题及相关内容复习
  8. 数据之路 - Python爬虫 - 数据存储
  9. java文件替换一行数据_用Golang替换文件中的一行
  10. 遇到这 4 个迹象,赶紧下来、让人工智能上!
  11. jmeter使用中的问题
  12. php -- 取日期
  13. HDU4757 Tree(可持久化Trie)
  14. 用Python将一个文件夹下多个子文件夹中相同文件拷贝到同一个文件夹中并重新命名
  15. 关于metasploit的一些架构目录
  16. EastFax传真服务器与单机传真软件什么区别
  17. Unity 3D 特效学习记录
  18. SAP中常用SE系列TCODE汇总
  19. 读取pb模型进行预测
  20. 变量与指针、取值符与取地址符

热门文章

  1. java实现pdf打印工具类,Java PDF工具类(二)| 使用 wkhtmltox 实现 HTML转PDF(文字/图片/页眉页脚)...
  2. abaqus失效单元删除_abaqus删除失效单元
  3. mysql扩展函数创建临时表_MySQL函数中创建临时表
  4. 台式计算机文件打不开怎么回事,电脑文件打不开怎么回事
  5. kafka安装_kafka 安装部署教程
  6. Android的cangoback方法,Android应用开发Android8.0 WebView返回上一层失效(canGoBack返回false问题)解决办法...
  7. 工厂模式的思想主要为
  8. Android登陆界面实现-支持输入框清楚和震动效果功能
  9. 【前端模板之路】二、人肉非智举,让代码帮我们写代码才是王道
  10. LINUX上传下载小工具lrzsz