C語言 將字串按照指定字元分離並且反轉(三級指標)列子

gaopengtttt發表於2017-01-03
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==NULL\n");
  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 error\n");
  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,i-m+1))==NULL)
  85.                         {
  86.                                 printf("strsplt error:mem calloc error\n");
  87.                                 goto err;
  88.                         }
  89.                         strncpy(*(temp+p),tempsrc,i-m);
  90.                         p++;
  91.                         m=i+1;
  92.                 }
  93.         }
  94.         //last copy
  95.         tempsrc=instr+m;
  96.         if( (*(temp+p)= (char*)calloc(1,i-m+1)) == NULL)
  97.         {
  98.                 printf("strsplt error:mem calloc error\n");
  99.                 goto err;
  100.         }
  101.         strncpy(*(temp+p),tempsrc,i-m);
  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=0\n");
  117.                 return -1;
  118.         }
  119.         for(i=0,j=templ-1;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 %d\n",slen);
  135.                 for(i=0;i<slen;i++)
  136.                 {
  137.                         printf("%s\n",*(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("%s\n",*(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 char\n");
  183.                 return 2;
  184.         }

  185.         strsplrev(argv[1],argv[2]);
  186.         return 0;


  187. }







來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/7728585/viewspace-2131812/,如需轉載,請註明出處,否則將追究法律責任。

相關文章