两个辅助指针变量挖字符串(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;  
}  



  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 字符串连接函数可以通过使用指向字符串指针变量实现。具体步骤如下: 1. 定义一个函数,函数名为strcat,参数为两个指向字符串指针变量,分别表示要连接的两个字符串。 2. 在函数内部,先定义一个指向字符的指针变量,用于指向第一个字符串的末尾。 3. 使用while循环,将第一个字符串的每个字符复制到指针变量所指向的位置,直到遇到字符串结束符'\0'。 4. 将第二个字符串的每个字符复制到指针变量所指向的位置,直到遇到字符串结束符'\0'。 5. 返回第一个字符串指针变量,即可得到连接后的字符串。 示例代码如下: char* strcat(char* str1, char* str2) { char* ptr = str1; while (*ptr != '\0') { ptr++; } while (*str2 != '\0') { *ptr = *str2; ptr++; str2++; } *ptr = '\0'; return str1; } ### 回答2: 字符串连接函数是指将若干个字符串按某规定顺序连接起来,形成一个新的字符串的函数。在C语言中,可以用指向字符串指针变量实现字符串连接函数。 首先,需要明确字符串连接函数的基本原理:将多个字符串按指定的顺序依次拼接起来,形成一个新的字符串。例如,连接字符串“hello”和“world”可以得到“helloworld”。 有了这个基本原理,可以开始着手实现字符串连接函数。具体步骤如下: 1. 定义一个字符数组,作为拼接后的字符串。可以先将其初始化为空字符串,例如: char result[1000] = ""; 2. 遍历输入的多个字符串,将它们按顺序依次拼接到这个字符数组中。可以使用一个指向字符数组的指针变量实现。例如: char* p = result; strcpy(p, str1);//将第一个字符串复制到result中 p += strlen(p);//将指针移动到字符串末尾 strcpy(p, str2);//将第二个字符串复制到result中 p += strlen(p);//将指针移动到字符串末尾 //以此类推,将多个字符串按顺序拼接到result中 3. 返回拼接后的字符串。由于result数组的大小是固定的,可以根据实际情况使用动态内存分配或者预定义较大的数组来避免字符串长度超过数组大小的情况。 这样,一个简单的字符串连接函数就实现了。当然,在实际应用中,还需要考虑字符串长度、内存分配和指针操作等方面的问题,这些细节和优化需要根据实际情况具体处理。 ### 回答3: 字符串连接是常见的应用需求,通常是将两个或多个字符串合并成一个字符串。在C语言中,可以通过字符串拼接函数strcat()实现字符串连接,但是这个函数有一些缺点,例如需要保证目标字符串有足够的空间来存放连接后的字符串,不能对目标字符串进行检查,会导致数组越界等问题。因此,使用指向字符串指针变量实现字符串连接是一个更加可靠和安全的方法。 使用指向字符串指针变量实现字符串连接需要自定义一个函数。首先,需要创建一个存放连接后字符串的内存空间,并且将源字符串复制到该空间中。然后,遍历剩余的源字符串,逐一将其拷贝到上一个字符串的结尾。最后,为连接后的字符串添加结尾符'\0'。 实现代码如下: ```c #include<stdio.h> #include<string.h> #define MAX_LENGTH 1000 char* mystrcat(char* res, const char* str) { char* temp = res; while(*temp != '\0') { temp++; } while((*temp++ = *str++)) {} return res; } int main() { char str1[20] = "Hello, "; char str2[20] = "world!"; char res[MAX_LENGTH] = ""; mystrcat(res, str1); mystrcat(res, str2); printf("%s\n", res); return 0; } ``` 在该代码中,使用mystrcat()函数实现字符串的连接操作。该函数接受两个参数,第一个参数为目标字符串指针,第二个参数为需要连接的字符串指针。在函数内部,首先使用一个指针变量temp指向目标字符串的结尾,然后逐一将需要连接的字符串中的字符拷贝到temp指针所指向的位置,直到遇到'\0'为止。最后,在连接后的字符串的末尾添加结尾符'\0'。在主函数中,创建了两个字符串str1和str2以及一个用于存放连接后字符串的数组res。调用mystrcat()函数,将str1和str2连接到res中,并通过printf()函数打印出连接后的字符串。 总之,使用指向字符串指针变量实现字符串连接函数不仅可以保证程序的稳定性和安全性,还可以自定义具有更好的灵活性和可扩展性的连接方式,适合一些特定的应用场景。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值