【C语言提高30】二级指针强化训练[02]
2015-12-06 23:40
495 查看
#include<stdlib.h> #include<stdio.h> #include<string.h> #include<ctype.h> //主调函数分配内存 char** spitString3(char*buf1, char c, int*count) { //strcpy(buf2[0], "aaaaa"); //strcpy(buf2[1], "bbbbbb"); char *p = NULL, *pTmp = NULL; int tmpcount = 0; char**myp; //1 p和ptmp初始化 p = buf1; pTmp = buf1; //第一遍扫描求出count //多少个逗号 //////////////////////////////////////////////////////////// do { //2 检索符合条件的位置 p后移 形成差值 挖字符串 p = strchr(p, c); if (p != NULL) { if (p - pTmp > 0) { strncpy(myp[tmpcount], pTmp, p - pTmp); myp[tmpcount][p - pTmp] = '\0'; //把第一行数据变成 C风格字符串 tmpcount++; //3重新 让p和ptmp达到下一次检索的条件 pTmp = p = p + 1; } } else { break; } } while (*p != '\0'); *count = tmpcount; //根据多少行 精确的分配内存 ///////////////////////////////////////////////////////// //1 p和ptmp初始化 p = buf1; pTmp = buf1; tmpcount = 0; myp = (char**)malloc(tmpcount*sizeof(char*)); if (myp == NULL) { return NULL; } do { //2 检索符合条件的位置 p后移 形成差值 挖字符串 p = strchr(p, c); if (p != NULL) { if (p - pTmp > 0) { int len = p - pTmp + 1; 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风格字符串 tmpcount++; //3重新 让p和ptmp达到下一次检索的条件 pTmp = p = p + 1; } } else { break; } } while (*p != '\0'); *count = tmpcount; return myp; } //主调函数分配内存 int spitString4(char*buf1, char c, char***myp3,int*count) { //strcpy(buf2[0], "aaaaa"); //strcpy(buf2[1], "bbbbbb"); char *p = NULL, *pTmp = NULL; int tmpcount = 0; char**myp; //1 p和ptmp初始化 p = buf1; pTmp = buf1; //第一遍扫描求出count //多少个逗号 //////////////////////////////////////////////////////////// do { //2 检索符合条件的位置 p后移 形成差值 挖字符串 p = strchr(p, c); if (p != NULL) { if (p - pTmp > 0) { strncpy(myp[tmpcount], pTmp, p - pTmp); myp[tmpcount][p - pTmp] = '\0'; //把第一行数据变成 C风格字符串 tmpcount++; //3重新 让p和ptmp达到下一次检索的条件 pTmp = p = p + 1; } } else { break; } } while (*p != '\0'); *count = tmpcount; //根据多少行 精确的分配内存 ///////////////////////////////////////////////////////// //1 p和ptmp初始化 p = buf1; pTmp = buf1; tmpcount = 0; myp = (char**)malloc(tmpcount*sizeof(char*)); if (myp == NULL) { return -2; } do { //2 检索符合条件的位置 p后移 形成差值 挖字符串 p = strchr(p, c); if (p != NULL) { if (p - pTmp > 0) { int len = p - pTmp + 1; myp[tmpcount] = (char*)malloc(len*sizeof(char)); if (myp[tmpcount == NULL]) { return -3; } strncpy(myp[tmpcount], pTmp, p - pTmp); myp[tmpcount][p - pTmp] = '\0'; //把第一行数据变成 C风格字符串 tmpcount++; //3重新 让p和ptmp达到下一次检索的条件 pTmp = p = p + 1; } } else { break; } } while (*p != '\0'); *count = tmpcount; *myp3 = myp; return 0; } void main() { int ret = 0, i = 0; char *p1 = "abcdef,acccd,eeee,aaaa,e3eeeee,sssss,"; char cTem = ','; int nCount; char **p = NULL; /* p = spitString3(p1, cTem, &nCount); if (p== NULL) { printf("fucn spitString() err: %d \n", ret); return; } */ ret = spitString4(p1, cTem, &p, &nCount); if (ret == NULL) { printf("fucn spitString() err: %d \n", ret); return; } for (i = 0; i<nCount; i++) { printf("%s \n", p); } for (i = 0; i < nCount; i++) { free(p[i]); } free(p); printf("hello...\n"); system("pause"); return; }
相关文章推荐
- 【C语言提高30】二级指针强化训练
- 怎样学习 C++ STL?
- LeetCode 231判断2的幂
- VS2008 C++ 项目怎样添加“依赖”、“库目录”和“包含目录”
- 二级指针的作用详解
- Arduino 平台与C语言程序设计-week3-Arduino Programs-Lesson3
- 《C++并发编程实战》读书笔记7---注意事项
- C语言程序设计-第6章 利用数组处理批量数据
- Arduino 平台与C语言程序设计-week3-Arduino Programs-Lesson2
- C语言基础(一)
- mysql Connector C/C++ 多线程封装
- 20151202 c语言小代码
- C语言基础,排序算法之冒泡排序算法
- Arduino 平台与C语言程序设计-week3-Arduino Programs-Lesson1
- MySQL Connector/C++入门教程(上)
- 求字符串的最长无重复字符子串(C++)
- C++容器类是什么意思? 简单易懂
- C++中容器跟模板有什么异同么?用简单易懂的话
- 读取Windows桌面图标的名字和位置,32bit程序读取64bit进程
- Arduino 平台与C语言程序设计-week2-C Programming-Lesson3