天天看点

C语言 将字符串按照指定字符分离并且反转(三级指针)列子

C语言 将字符串分离并且反转(三级指针)

本程序完成功能

1、将输入的字符串按照指定字符分离为子字符串

2、将子字符串进行反转

使用方法

在栈空间分配一个三级指针,指向堆内存空间的指针数组的位置,每个指针数组成员又指向一个字符串,必须明确如下的

内存四区图这里只画最为复杂的分离字符函数,而不画反转函数,因为反转函数模型非常简单,而且画太多太麻烦。

在图中反应出strsplrev中的int len和 char** str为输出变量,也就是说str用来接受malloc出来的二级指针的值,那么我们应该

将三级指针传入,另外argv输入的字符串放到了全局区,这个区域是不能更改的,所以用一个堆instr将他接收过来,这也是典

型的场景下图(图中将i m p3个栈变量放到了一起),重点在于堆中的内存理解问题。

程序输出如下:

gaopeng@bogon:~/stepup/3rd$ ./splitstr oracle-mysql-mongodb -

----len is 3

oracle

mysql

mongodb

----reverse now!

elcaro

lqsym

bdognom

----free mem!

四区图如下:

C语言 将字符串按照指定字符分离并且反转(三级指针)列子

最后给出实现代码:

点击(此处)折叠或打开

接口头文件:

/*************************************************************************

    > File Name: fun.h

    > Author: gaopeng QQ:22389860 all right reserved

    > Mail: [email protected]

    > Created Time: Tue 24 Jan 2017 03:50:07 PM CST

 ************************************************************************/

int strsplt(const char* instrt/*in*/,const char* spchar/*in*/,char** *outp/*out*/,int* len/*out*/);

int strrevs(char* instrt/*in out*/);

int strsplrev(char *p1,char *p2);

接口实现文件:

  > File Name: fun.c

  > Author: gaopeng QQ:22389860 all right reserved

  > Mail: [email protected]

  > Created Time: Tue 24 Jan 2017 02:02:20 PM CST

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

int mfree(char** *freep,int len)

{

        int i=0;

        if(*freep==NULL)

        {

                return 0;

        }

        for(i=0;i<len;i++)

                if(*(*freep+i) !=NULL)

                {

                        printf("%d ",i);

                        free(*(*freep+i));

                        *(*freep+i)=NULL;

                }

        free(*freep);

        freep = NULL;

        return 0;

}

/*-1 flase 0 ture*/

int strsplt( char* instr,const char* spchar,char** *outp,int* len )

        char** temp=NULL;

        int i=0,m=0,p=0;

        int templen=0;

        char* tempsrc=NULL;

        if(instr==NULL||spchar==NULL||outp==NULL||len==NULL)

                printf("strsplt error:instr==NULL||spchar==NULL||outp==NULL||len==NULL\n");

                goto err;

        p = 1;

        for(i=0;i<strlen(instr);i++)

                if(instr[i]==spchar[0])

                        if (i == strlen(instr)-1)

                        {

                                printf("strsplt error:last char is spchar \n");

                                goto err;

                        }

                        if(instr[i+1]==spchar[0])

                                printf("strsplt error:two or more spchar \n");

                        p++;

        *len = p;

        templen = p;//used mfree fun

        if((temp=(char**)calloc(1,p*sizeof(char*)))==NULL)

                printf("strsplt error:mem calloc error\n");

        *outp=temp;

        for(i=0,p=0,m=0;i<strlen(instr);i++)//i is step p is step of pointer m is len of substr

                        tempsrc=instr+m;

                        if( (*(temp+p)= (char*)calloc(1,i-m+1))==NULL)

                                printf("strsplt error:mem calloc error\n");

                        strncpy(*(temp+p),tempsrc,i-m);

                        m=i+1;

        //last copy

        tempsrc=instr+m;

        if( (*(temp+p)= (char*)calloc(1,i-m+1)) == NULL)

        strncpy(*(temp+p),tempsrc,i-m);

        err:

        mfree(&temp,templen);

        return -1;

/* -1 false 0 true*/

int strrevs(char* instrt)

        int templ=0;

        int i=0,j=0;

        char tempc=0;

        templ = strlen(instrt);

        if(instrt == NULL || templ==0)

                printf("strrevs error:instrt == NULL || templ=0\n");

                return -1;

        for(i=0,j=templ-1;i<j;i++,j--)

                tempc=instrt[j];

                instrt[j]=instrt[i];

                instrt[i]=tempc;

int strsplrev(char *p1,char *p2)

        char* *str=NULL;

        int slen=0;

        if(strsplt(p1,p2,&str,&slen) == 0)

                printf("----len is %d\n",slen);

                for(i=0;i<slen;i++)

                        printf("%s\n",*(str+i));

        else

                printf("error!\n");

                return 3;

        printf("----reverse now!\n");

        for(i=0;i<slen;i++)

                if(strrevs(*(str+i)) !=0)

                        printf("error!\n");

                        return 4;

                else

        printf("----free mem!\n");

        mfree(&str,slen);

main调用文件:

  > File Name: main.c

  > Created Time: Tue 24 Jan 2017 02:02:09 PM CST

#include"fun.h"

int main(int argc,char* argv[])

        if(argc!=3||strcmp(argv[1],"--help")==0)

                printf("usage ./tool str spchar \n");

                return 1;

        if(strlen(argv[2]) !=1)

                printf("par2 split char only one char\n");

                return 2;

        strsplrev(argv[1],argv[2]);

继续阅读