Chinaunix首页 | 论坛 | 博客
  • 博客访问: 84516
  • 博文数量: 19
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 281
  • 用 户 组: 普通用户
  • 注册时间: 2013-08-07 14:42
个人简介

业精于勤,荒于嬉;凡事用心,事事皆成。

文章分类

全部博文(19)

文章存档

2017年(1)

2016年(9)

2015年(2)

2014年(2)

2013年(5)

我的朋友

分类: C/C++

2016-04-28 23:08:42

4、编写一个方法,将字符串中的空格全部替换为“%20”,假定该字符串尾部有足够的空间存放新增字符,并且知道字符的“真实”长度。


点击(此处)折叠或打开

  1. // Replace blank with %20
  2. void ReplaceBlankString(char* str){
  3.      if(!str) return;

  4.      // check blank count
  5.      char* strTemp = str;
  6.      int nBlankCount = 0;
  7.      while(*strTemp){
  8.         if(*strTemp++ == ' ') nBlankCount++;
  9.      }

  10.      // the end of string
  11.      int nEnd = strlen(str) + 2 * nBlankCount;

  12.      char* end = str + nEnd;
  13.      // Begin move
  14.      while(end - strTemp > 0){
  15.         if(*strTemp == ' '){
  16.            *end-- = '0';
  17.            *end-- = '2';
  18.            *end-- = '%';
  19.         }
  20.         else{
  21.            *end-- = *strTemp;
  22.         }

  23.         strTemp--;
  24.      }
  25. }

完整代码请查看以下路径:
阅读(707) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~