两个辅助指针变量挖字符串(4种实现方式)

本文探讨了如何利用两个辅助指针在字符串处理中进行操作,结合内存模型图,介绍了4种不同的实现方式,重点讲解了在主调函数分配内存的情况。

两个辅助指针挖字符串

内存模型图


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;  
}  



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值