Chinaunix首页 | 论坛 | 博客
  • 博客访问: 415960
  • 博文数量: 45
  • 博客积分: 4075
  • 博客等级: 上校
  • 技术积分: 666
  • 用 户 组: 普通用户
  • 注册时间: 2007-04-24 18:09
个人简介

百度网页搜索部高级工程师 我的微博:http://weibo.com/pengwh85

文章分类

全部博文(45)

文章存档

2012年(3)

2011年(1)

2010年(19)

2009年(10)

2008年(12)

我的朋友

分类: C/C++

2010-04-07 12:50:54

算法很简单,不用多说。
 
/* --------------- String Trimming --------------- */
 

#include

#include

void TrimStr(const char *oriStr, char *resStr);

int CountStrLen(const char *oriStr);

int main()

{

    const char *oriStr = " a xxx e a b e w ah ";

    const int strLen = CountStrLen(oriStr);

    char *resStr = (char *) malloc(strLen + 1);

    TrimStr(oriStr, resStr);

    printf("Original: %s\nNew: %s\n", oriStr, resStr);

 

    free(resStr);

    return 0;

}

void TrimStr(const char *oriStr, char *resStr)

{

    const char *p = oriStr;

    while (*p)

    {

        if (*p != ' ')

        {

            *resStr++ = *p;

        }

        ++p;

    }

    *resStr = '\0';

}

int CountStrLen(const char *oriStr)

{

    const char *p = oriStr;

    int i = 0;

    while (*p++)

    {

        ++i;

    }

    return i;

}

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

上一篇:C#中yield的作用

下一篇:C++堆排序

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