trim函数的C语言实现

来源:互联网 发布:韩国明星学霸 知乎 编辑:程序博客网 时间:2024/04/27 23:50

   
  #include   <stdio.h>  
  #include   <string.h>  
  #include   <malloc.h>  
   
  char   *   strtrim(char   *sDes,   const   char   *sSrc   )  
  {  
      char   *temp   =   sDes;  
      while   (*sSrc++   ==   '   ');  
      sSrc--;  
      while   ((*sDes++   =   *sSrc++)   !=   0x00);  
      sDes--;  
      while   (*--sDes   ==   '   ');  
      *++sDes   =   0x00;  
      return   temp;  
  }  
   
  void   main()  
  {  
   
  char   *sString   =   "         1               ";  
  char   sTrim[6];  
  strtrim(sTrim,   sString);  
  printf("*%s*/n",   sString);  
  printf("*%s*/n",   sTrim);    
  }  

 

 

char*   trim(   const   char*   s   )  
  {  
          static   char   rel[MAX_LEN];  
          char*   se   =   0;  
          assert(   s   );  
          assert(   strlen(   s   )   <=   MAX_LEN   &&   strlen(   s   )   !=   0   );  
          se   =   s   +   strlen(   s   )   -   1;  
          for(   ;   isspace(   *s   )   ;   ++s   );  
          for(   ;   isspace(   *se   )   ;   --se   );  
          strncpy(   rel   ,   s   ,   se   -   s   );  
          return   rel;  
  }

原创粉丝点击