1. 首页
  2. IT资讯

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!

四区图如下:

最后给出实现代码:

点击(此处)折叠或打开

  1. 接口头文件:
  2. /*************************************************************************
  3.     > File Name: fun.h
  4.     > Author: gaopeng QQ:22389860 all right reserved
  5.     > Mail: gaopp_200217@163.com
  6.     > Created Time: Tue 24 Jan 2017 03:50:07 PM CST
  7.  ************************************************************************/
  8. int strsplt(const char* instrt/*in*/,const char* spchar/*in*/,char** *outp/*out*/,int* len/*out*/);
  9. int strrevs(char* instrt/*in out*/);
  10. int strsplrev(char *p1,char *p2);
  11. 接口实现文件:
  12. /*************************************************************************
  13.   > File Name: fun.c
  14.   > Author: gaopeng QQ:22389860 all right reserved
  15.   > Mail: gaopp_200217@163.com
  16.   > Created Time: Tue 24 Jan 2017 02:02:20 PM CST
  17.  ************************************************************************/
  18. #include<stdio.h>
  19. #include<stdlib.h>
  20. #include<string.h>
  21. int mfree(char** *freep,int len)
  22. {
  23.         int i=0;
  24.         if(*freep==NULL)
  25.         {
  26.                 return 0;
  27.         }
  28.         for(i=0;i<len;i++)
  29.         {
  30.                 if(*(*freep+i) !=NULL)
  31.                 {
  32.                         printf(“%d “,i);
  33.                         free(*(*freep+i));
  34.                         *(*freep+i)=NULL;
  35.                 }
  36.         }
  37.         free(*freep);
  38.         freep = NULL;
  39.         return 0;
  40. }
  41. /*1 flase 0 ture*/
  42. int strsplt( char* instr,const char* spchar,char** *outp,int* len )
  43. {
  44.         char** temp=NULL;
  45.         int i=0,m=0,p=0;
  46.         int templen=0;
  47.         char* tempsrc=NULL;
  48.         if(instr==NULL||spchar==NULL||outp==NULL||len==NULL)
  49.         {
  50.                 printf(“strsplt error:instr==NULL||spchar==NULL||outp==NULL||len==NULLn”);
  51.                 goto err;
  52.         }
  53.         p = 1;
  54.         for(i=0;i<strlen(instr);i++)
  55.         {
  56.                 if(instr[i]==spchar[0])
  57.                 {
  58.                         if (i == strlen(instr)1)
  59.                         {
  60.                                 printf(“strsplt error:last char is spchar n”);
  61.                                 goto err;
  62.                         }
  63.                         if(instr[i+1]==spchar[0])
  64.                         {
  65.                                 printf(“strsplt error:two or more spchar n”);
  66.                                 goto err;
  67.                         }
  68.                         p++;
  69.                 }
  70.         }
  71.         *len = p;
  72.         templen = p;//used mfree fun
  73.         if((temp=(char**)calloc(1,p*sizeof(char*)))==NULL)
  74.         {
  75.                 printf(“strsplt error:mem calloc errorn”);
  76.                 goto err;
  77.         }
  78.         *outp=temp;
  79.         for(i=0,p=0,m=0;i<strlen(instr);i++)//i is step p is step of pointer m is len of substr
  80.         {
  81.                 if(instr[i]==spchar[0])
  82.                 {
  83.                         tempsrc=instr+m;
  84.                         if( (*(temp+p)= (char*)calloc(1,im+1))==NULL)
  85.                         {
  86.                                 printf(“strsplt error:mem calloc errorn”);
  87.                                 goto err;
  88.                         }
  89.                         strncpy(*(temp+p),tempsrc,im);
  90.                         p++;
  91.                         m=i+1;
  92.                 }
  93.         }
  94.         //last copy
  95.         tempsrc=instr+m;
  96.         if( (*(temp+p)= (char*)calloc(1,im+1)) == NULL)
  97.         {
  98.                 printf(“strsplt error:mem calloc errorn”);
  99.                 goto err;
  100.         }
  101.         strncpy(*(temp+p),tempsrc,im);
  102.         return 0;
  103.         err:
  104.         mfree(&temp,templen);
  105.         return 1;
  106. }
  107. /* 1 false 0 true*/
  108. int strrevs(char* instrt)
  109. {
  110.         int templ=0;
  111.         int i=0,j=0;
  112.         char tempc=0;
  113.         templ = strlen(instrt);
  114.         if(instrt == NULL || templ==0)
  115.         {
  116.                 printf(“strrevs error:instrt == NULL || templ=0n”);
  117.                 return 1;
  118.         }
  119.         for(i=0,j=templ1;i<j;i++,j)
  120.         {
  121.                 tempc=instrt[j];
  122.                 instrt[j]=instrt[i];
  123.                 instrt[i]=tempc;
  124.         }
  125.         return 0;
  126. }
  127. int strsplrev(char *p1,char *p2)
  128. {
  129.         char* *str=NULL;
  130.         int slen=0;
  131.         int i=0;
  132.         if(strsplt(p1,p2,&str,&slen) == 0)
  133.         {
  134.                 printf(“—-len is %dn”,slen);
  135.                 for(i=0;i<slen;i++)
  136.                 {
  137.                         printf(“%sn”,*(str+i));
  138.                 }
  139.         }
  140.         else
  141.         {
  142.                 printf(“error!n”);
  143.                 return 3;
  144.         }
  145.         printf(“—-reverse now!n”);
  146.         for(i=0;i<slen;i++)
  147.         {
  148.                 if(strrevs(*(str+i)) !=0)
  149.                 {
  150.                         printf(“error!n”);
  151.                         return 4;
  152.                 }
  153.                 else
  154.                 {
  155.                         printf(“%sn”,*(str+i));
  156.                 }
  157.         }
  158.         printf(“—-free mem!n”);
  159.         mfree(&str,slen);
  160.         return 0;
  161. }
  162. main调用文件:
  163. /*************************************************************************
  164.   > File Name: main.c
  165.   > Author: gaopeng QQ:22389860 all right reserved
  166.   > Mail: gaopp_200217@163.com
  167.   > Created Time: Tue 24 Jan 2017 02:02:09 PM CST
  168.  ************************************************************************/
  169. #include<stdio.h>
  170. #include<stdlib.h>
  171. #include<string.h>
  172. #include“fun.h”
  173. int main(int argc,char* argv[])
  174. {
  175.         if(argc!=3||strcmp(argv[1],“–help”)==0)
  176.         {
  177.                 printf(“usage ./tool str spchar n”);
  178.                 return 1;
  179.         }
  180.         if(strlen(argv[2]) !=1)
  181.         {
  182.                 printf(“par2 split char only one charn”);
  183.                 return 2;
  184.         }
  185.         strsplrev(argv[1],argv[2]);
  186.         return 0;
  187. }

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/7728585/viewspace-2131812/,如需转载,请注明出处,否则将追究法律责任。

主题测试文章,只做测试使用。发布者:深沉的少年,转转请注明出处:http://www.cxybcw.com/183814.html

联系我们

13687733322

在线咨询:点击这里给我发消息

邮件:1877088071@qq.com

工作时间:周一至周五,9:30-18:30,节假日休息

QR code