两个辅助指针变量挖字符串(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
原创粉丝点击