Chinaunix首页 | 论坛 | 博客
  • 博客访问: 416541
  • 博文数量: 73
  • 博客积分: 3326
  • 博客等级: 中校
  • 技术积分: 631
  • 用 户 组: 普通用户
  • 注册时间: 2010-02-05 15:31
文章分类

全部博文(73)

文章存档

2014年(1)

2011年(51)

2010年(21)

分类: C/C++

2011-08-03 20:41:24

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

  4. typedef int bool;
  5. #define false 0
  6. #define true 1

  7. /* del the blank characters from string */
  8. void deblank(char string[])
  9. {
  10.         char *string_temp = string;
  11.         int i, j;
  12.         for (i = 0, j = 0; string_temp[i] != '\0'; )
  13.         {
  14.                 string[j++] = string_temp[i++];
  15.                 while (string_temp[i] == ' ')
  16.                 {
  17.                         i++;
  18.                 }
  19.         }
  20.         string[j] = '\0';
  21. }

  22. void deblank2(char string[])
  23. {
  24.     char *str=string;
  25.     int i=0,j=0;

  26.     while(str[i]!='\0')
  27.     {
  28.         if(isspace(str[i])&&isspace(str[i+1]))//str[i] is space
  29.         {
  30.             string[j]=str[i];
  31.             i++;
  32.         }
  33.         else
  34.         {
  35.             string[j]=str[i];
  36.         }
  37.         j++;
  38.         i++;
  39.     }
  40.     string[j]='\0';
  41. }
  42. /* The Best One */
  43. void trimStr(char *src)
  44. {
  45.     char *p = src;
  46. //    char tmp[]={0};
  47.     char *tmp = src;
  48.     bool flag = false;

  49.     int i;
  50.     if(!*p)
  51.     {
  52.         printf("Null string\n");
  53.         return;
  54.     }

  55.     for(i = 0; *p; p++)
  56.     {
  57.         if(*p != ' ' )
  58.         {
  59.             tmp[i++] = *p;
  60.             flag = true;
  61.         }
  62.         else if(flag)
  63.         {
  64.             tmp[i++]=*p;
  65.             flag = false;
  66.         }
  67.         else
  68.         {
  69.             continue;
  70.         }

  71.     }

  72.     tmp[i] = '\0';
  73.     strcpy(src,tmp);
  74. }

  75. void trim(char *s)
  76. {
  77.     char *p;
  78.     for(p = s; *s; s ++)
  79.     {
  80.         if(*s != ' ')
  81.         {    
  82.             *p ++ = *s;
  83.         }
  84.     }

  85. }

  86. int main()
  87. {
  88.     char s[] = "That is a kind of beatiful dunk! ";
  89.     char ds[]={0};
  90.     printf("Before del blank:\n");
  91.     printf("%s\n",s);
  92.     
  93.     //deblank(s);
  94.     //deblank2(s);
  95.     trimStr(s);
  96. //    trim(s);

  97.     printf("After del blank:\n");
  98.     printf("%s\n",s);
  99.     
  100.     return 0;
  101. }
阅读(931) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~