两个辅助指针变量挖字符串(4种实现方式)
来源:互联网 发布:linux diff命令的功能 编辑:程序博客网 时间:2024/06/06 19:24
两个辅助指针挖字符串
内存模型图
1) 使用第二种内存模型 主调函数分配内存
#include<stdio.h> #include<stdlib.h> #include <string.h> #include <ctype.h> /*函数功能 根据字符c来分割字符串str*/ int splitStr(const char *str,char c ,char buf[10][20],int *num) { char* p = NULL,*pTmp = NULL;//两个辅助指针变量 int tmpcount = 0,len; p = str; pTmp = str; do { p = strchr(p,c); if (p!=NULL) { if (p-pTmp>0) { strncpy(buf[tmpcount],pTmp,p-pTmp); buf[tmpcount][p-pTmp] = '\0'; //printf("%s\n",buf[tmpcount]); tmpcount++; pTmp = p = p+1; len = strlen(p);//用来保存最后一个字符串的长度 } } else { //拷贝最后一个分割的字符串包括\0 strncpy(buf[tmpcount],pTmp,len+1); break; } } while (*p!='\0'); *num=tmpcount+1; return 0; } /*函数功能 打印二维数组*/ void printArr(char a[10][20],int n) { int i; for (i=0;i<n;i++) { printf("%s\n",*(a+i)); } } int main() { char *input="abcdefg,hjkln,sssss,kkk,hhh,j"; char ctemp = ','; char myArr[10][20]={0}; char *tmp; int ret; int n,i; ret = splitStr(input,ctemp,myArr,&n); if (ret!=0) { printf("error\n"); } printArr(myArr,n); system("pause"); return 0; }2)使用第三种内存模型 主调函数分配内存
#include<stdio.h> #include<stdlib.h> #include <string.h> #include <ctype.h> /*第三种内存模型*/ int splitStr(const char *str,char c,char **buf,int *count) { char* p = NULL,*pTmp = NULL;//两个辅助指针变量 int tmpcount = 0,len; p = str; pTmp = str; do { p = strchr(p,c); if (p!=NULL) { if (p-pTmp>0) { strncpy(buf[tmpcount],pTmp,p-pTmp); buf[tmpcount][p-pTmp] = '\0'; //printf("%s\n",buf[tmpcount]); tmpcount++; pTmp = p = p+1; len = strlen(p);//用来保存最后一个字符串的长度 } } else { //拷贝最后一个分割的字符串包括\0 strncpy(buf[tmpcount],pTmp,len+1); break; } } while (*p!='\0'); *count=tmpcount+1; return 0; } int main() { char *input="abcdefg,hjkln,sssss,kkk,hhh,j"; char ctemp = ','; char **p = NULL; int ret; int n,i; p=(char**)malloc(10*sizeof(char*)); if (p==NULL) { return; } for (i=0;i<10;i++) { p[i] = (char *)malloc(20*sizeof(char)); } ret = splitStr(input,ctemp,p,&n); if (ret!=0) { printf("error\n"); } for (i=0;i<n;i++) { printf("%s\n",*(p+i)); } //释放内存 system("pause"); return 0; }
3)使用第三种内存模型 被调函数分配内存 通过return返回
#include<stdio.h> #include<stdlib.h> #include <string.h> #include <ctype.h> /*第三种内存模型 被调函数分配内存 通过return返回*/ char** splitStr2(char *str,char c,int *count) { char* p = NULL,*pTmp = NULL;//两个辅助指针变量 int tmpcount = 0,len,len2; char**buf; p = str; pTmp = str; //第一遍扫描 开辟第一维空间 do { p = strchr(p,c); if (p!=NULL) { if (p-pTmp>0) { tmpcount++; pTmp = p = p+1; } } else { break; } } while (*p!='\0'); *count=tmpcount+1; buf = (char**)malloc((tmpcount+1)*sizeof(char*)); //printf("tmpcount:%d\n",tmpcount); if(buf==NULL) { return NULL; } //第二遍扫描 根据分割的字符串的长度,开辟第二维空间并拷贝字符串 tmpcount = 0; p = str; pTmp = str; do { p = strchr(p,c); if (p!=NULL) { if (p-pTmp>0) { len = p-pTmp+1; buf[tmpcount] = (char*)malloc(len*sizeof(char)); if (buf[tmpcount] == NULL) { free(buf); return ; } strncpy(buf[tmpcount],pTmp,p-pTmp); buf[tmpcount][p-pTmp] = '\0'; tmpcount++; pTmp = p = p+1; len2 = strlen(p);//用来保存最后一个字符串的长度 } } else { //printf("tmpcount:%d\n",tmpcount); //printf("len2:%d\n",len2); buf[tmpcount] = (char*)malloc((len2+1)*sizeof(char)); if (buf[tmpcount] == NULL) { free(buf); return ; } //拷贝最后一个分割的字符串包括\0 strncpy(buf[tmpcount],pTmp,len2+1); break; } } while (*p!='\0'); return buf; } int main() { char *input="abcdefg,hjkln,sssss,kkk,hhh,j"; char ctemp = ','; char **p = NULL; int ret; int n,i; p = splitStr2(input,ctemp,&n); printf("n:%d\n",n); for (i=0;i<n;i++) { printf("%s\n",*(p+i)); } //释放内存空间 system("pause"); return 0; }
4)使用第三种内存模型 被调函数分配内存 通过函数参数返回
#include<stdlib.h> #include <string.h> #include <ctype.h> /*第三种内存模型 被调函数分配内存 通过tmpbuf返回*/ int splitStr3(char *str,char c,char*** tmpbuf,int *count) { char* p = NULL,*pTmp = NULL;//两个辅助指针变量 int tmpcount = 0,len,len2; char**buf; p = str; pTmp = str; //第一遍扫描 开辟第一维空间 do { p = strchr(p,c); if (p!=NULL) { if (p-pTmp>0) { tmpcount++; pTmp = p = p+1; } } else { break; } } while (*p!='\0'); *count=tmpcount+1; buf = (char**)malloc((tmpcount+1)*sizeof(char*)); //printf("tmpcount:%d\n",tmpcount); if(buf==NULL) { return -1; } //第二遍扫描 根据分割的字符串的长度,开辟第二维空间并拷贝字符串 tmpcount = 0; p = str; pTmp = str; do { p = strchr(p,c); if (p!=NULL) { if (p-pTmp>0) { len = p-pTmp+1; buf[tmpcount] = (char*)malloc(len*sizeof(char)); if (buf[tmpcount] == NULL) { free(buf); return -2; } strncpy(buf[tmpcount],pTmp,p-pTmp); buf[tmpcount][p-pTmp] = '\0'; tmpcount++; pTmp = p = p+1; len2 = strlen(p);//用来保存最后一个字符串的长度 } } else { //printf("tmpcount:%d\n",tmpcount); //printf("len2:%d\n",len2); buf[tmpcount] = (char*)malloc((len2+1)*sizeof(char)); if (buf[tmpcount] == NULL) { free(buf); return -2; } //拷贝最后一个分割的字符串包括\0 strncpy(buf[tmpcount],pTmp,len2+1); break; } } while (*p!='\0'); *tmpbuf = buf; return 0; } //释放内存空间 void freeMem(char**p,int count) { int i =0; if (p == NULL) { return ; } for (i=0;i<count;i++) { if (p[i]!=NULL) { free(p[i]); } } if (p!=NULL) { free(p); } } int main() { char *input="abcdefg,hjkln,sssss,kkk,hhh,j"; char ctemp = ','; char **p = NULL; int ret; int n,i; ret = splitStr3(input,ctemp,&p,&n); if (ret!= 0) { printf("error"); return -1; } //printf("n:%d\n",n); for (i=0;i<n;i++) { printf("%s\n",*(p+i)); } //释放内存 freeMem(p,n); system("pause"); return 0; }
0 0
- 两个辅助指针变量挖字符串(4种实现方式)
- 【C语言提高29】两个辅助指针变量挖字符串
- 三种方式实现两个变量互换
- 在字符串copy函数中引入辅助指针变量
- 不用辅助变量实现两个整数之间值的交换
- 两个字符串相加用指针实现
- C++的基础知识(十六)--在字符串copy函数中引入辅助指针变量
- 指针交换两个变量
- 交换两个变量的三种方式
- 两个变量交换的几种方式
- 不使用辅助变量的两个变量交换
- 指针变量输入字符串
- 字符串指针变量
- 引用为参数实现两个字符串变量的交换
- 两个指针变量的减法
- 两个变量实现交换
- 两个变量实现交换
- 两个字符串复制(指针)
- 去除CSDN 博客页广告的历程
- JS中各种宽度距离小结
- [数据库连接池] Java数据库连接池--DBCP浅析.
- Chrome“无法添加来自此网站的应用”的解决办法
- [HDU]5531 Rebuild (三分法求下凸函数,维护最小圆面积)
- 两个辅助指针变量挖字符串(4种实现方式)
- C/C++野指针
- 1.mysql bin-log日志基本操作与数据库备份还原
- scipy求一些统计量的p值和分位数
- C Primer Plus学习 十六 函数概述
- android中data binding的使用
- viewpager过渡页
- SQL存储过程返回受影响的行数
- 关于用java实现RMI通信的项目,MeetingRMI