Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1177235
  • 博文数量: 573
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 66
  • 用 户 组: 普通用户
  • 注册时间: 2016-06-28 16:21
文章分类

全部博文(573)

文章存档

2018年(3)

2016年(48)

2015年(522)

分类: C/C++

2015-12-02 17:00:27


点击(此处)折叠或打开

  1. #include <string.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>

  4. char * pAllTrim(char * pstr)
  5. {
  6.     if(NULL == pstr)
  7.     {
  8.         return NULL;
  9.     }
  10.     char * phead = pstr;
  11.     char * ptail = pstr + strlen(pstr) - 1;
  12.     while((*phead == ' ')&&(phead < ptail)) phead++;
  13.     while((*ptail == ' ')&&(ptail > phead)) ptail--;
  14.     *(ptail + 1) = '\0';
  15.     memcpy(pstr, phead, sizeof(char)*(ptail - phead + 2));
  16.     return pstr;
  17. }

  18. char * pLeftTrim(char * pstr)
  19. {
  20.     if(NULL == pstr)
  21.     {
  22.         return NULL;
  23.     }
  24.     char * phead = pstr;
  25.     char * ptail = pstr + strlen(pstr) - 1;
  26.     while((*phead == ' ')&&(phead < ptail)) phead++;
  27.     *(ptail+1) = '\0';
  28.     memcpy(pstr, phead, sizeof(char)*(ptail - phead + 2));
  29.     return pstr;
  30. }

  31. char * pRightTrim(char * pstr)
  32. {
  33.      if(NULL == pstr)
  34.         {
  35.             return NULL;
  36.         }
  37.         char * phead = pstr;
  38.         char * ptail = pstr + strlen(pstr)-1;
  39.         while((*ptail == ' ')&&(ptail > phead)) ptail--;
  40.         *(ptail+1) = '\0';
  41.         memcpy(pstr, phead, sizeof(char)*(ptail - phead + 2));
  42.         return pstr;
  43. }

  44. char * pStrTrim(char * pstr)
  45. {
  46.     if(NULL == pstr)
  47.     {
  48.         return NULL;
  49.     }
  50.     char * phead = pstr;
  51.     char * ptemp = NULL;
  52.     while(*phead != '\0')
  53.     {
  54.         if(*phead == ' ')
  55.         {
  56.             ptemp = phead;
  57.             while((*ptemp++ = *(ptemp + 1)) != '\0');
  58.         }
  59.         else
  60.             phead++;
  61.     }
  62.     return pstr;
  63. }
  64. int main(char * argc, char ** argv)
  65. {
  66.     char szSrc1[1024] = " abc def jhhkj ";
  67.     char szSrc2[1024] = " abc def jhhkj ";
  68.     char szSrc3[1024] = " abc def jhhkj ";
  69.     char szSrc4[1024] = " abc def jhhkj ";
  70.     printf("szSrc1 = [%s]\n" , szSrc1);
  71.     printf("szSrc2 = [%s]\n" , szSrc2);
  72.     printf("szSrc3 = [%s]\n" , szSrc3);
  73.     printf("szSrc4 = [%s]\n" , szSrc4);
  74.     char *pLeft = NULL, *pRight = NULL, *pAll = NULL, *pStr = NULL;
  75.     pStr = pStrTrim(szSrc1);
  76.     pAll = pAllTrim(szSrc2);
  77.     pLeft = pLeftTrim(szSrc3);
  78.     pRight = pRightTrim(szSrc4);
  79.     printf("pStr = [%s]\n", pStr);
  80.     printf("pAll = [%s]\n", pAll);
  81.     printf("pLeft = [%s]\n", pLeft);
  82.     printf("pRight = [%s]\n", pRight);
  83.     return 0;
  84. }

阅读(665) | 评论(0) | 转发(0) |
0

上一篇:system函数

下一篇:umask函数

给主人留下些什么吧!~~