操作两个指针,一个处理原来的字符串,一个处理新字符串,前者不碰到空格的话,就与后者同步。

否则后者增加%20.

代码如下:

#include<iostream>
#include<string>
using namespace std;/*length 为字符数组string的总容量*/
void ReplaceBlank(char string[], int length)
{if (string == NULL && length <= 0)return;/*originalLength 为字符串string的实际长度*/int originalLength = 0;int numberOfBlank = 0;int i = 0;while (string[i] != '\0'){++originalLength;//统计字符串长度if (string[i] == ' ')++numberOfBlank;//统计字符串中的空格数量++i;}/*newLength 为把空格替换成'%20'之后的长度*/int newLength = (originalLength -numberOfBlank) + numberOfBlank * 3;// 因为这里空格也占据一个位置if (newLength > length)return;int indexOfOriginal = originalLength;//原始字符串的下标int indexOfNew = newLength;//新的字符串的下标while (indexOfOriginal >= 0 && indexOfNew > indexOfOriginal){if (string[indexOfOriginal] == ' '){string[indexOfNew--] = '0';//这里是先赋值,然后-1,下同string[indexOfNew--] = '2';string[indexOfNew--] = '%';--indexOfOriginal;}else{string[indexOfNew--] = string[indexOfOriginal--];}//--indexOfOriginal;}
}void Test(char* testName, char string[], int length, char expected[])
{if (testName != NULL)printf("%s begins: ", testName);ReplaceBlank(string, length);if (expected == NULL && string == NULL)printf("passed.\n");else if (expected == NULL && string != NULL)printf("failed.\n");else if (strcmp(string, expected) == 0) {cout << "string=" << string << endl;cout << "expected=" << expected << endl;printf("passed.\n");}else{cout << "string=" << string << endl;cout << "expected=" << expected << endl;printf("failed.\n");}
}// 空格在句子中间
void Test1()
{const int length = 100;char string[length] = "hello world";Test("Test1", string, length, "hello%20world");
}// 空格在句子开头
void Test2()
{const int length = 100;char string[length] = " helloworld";Test("Test2", string, length, "%20helloworld");
}// 空格在句子末尾
void Test3()
{const int length = 100;char string[length] = "helloworld ";Test("Test3", string, length, "helloworld%20");
}// 连续有两个空格
void Test4()
{const int length = 100;char string[length] = "hello  world";Test("Test4", string, length, "hello%20%20world");
}// 传入NULL
void Test5()
{Test("Test5", NULL, 0, NULL);
}// 传入内容为空的字符串
void Test6()
{const int length = 100;char string[length] = "";Test("Test6", string, length, "");
}//传入内容为一个空格的字符串
void Test7()
{const int length = 100;char string[length] = " ";Test("Test7", string, length, "%20");
}// 传入的字符串没有空格
void Test8()
{const int length = 100;char string[length] = "helloworld";Test("Test8", string, length, "helloworld");
}// 传入的字符串全是空格
void Test9()
{const int length = 100;char string[length] = "   ";Test("Test9", string, length, "%20%20%20");
}int main()
{Test1();Test2();Test3();Test4();Test5();Test6();Test7();Test8();Test9();cin.get();cin.get();return 0;
}

也可以是:

#include<iostream>
#include<string>
using namespace std;/*length 为字符数组string的总容量*/
void ReplaceBlank(char string[], int length)
{if (string == NULL && length <= 0)return;/*originalLength 为字符串string的实际长度*/int originalLength = 0;int numberOfBlank = 0;int i = 0;while (string[i] != '\0'){++originalLength;//统计字符串长度if (string[i] == ' ')++numberOfBlank;//统计字符串中的空格数量++i;}/*newLength 为把空格替换成'%20'之后的长度*/int newLength = (originalLength -numberOfBlank) + numberOfBlank * 3;// 因为这里空格也占据一个位置if (newLength > length)return;int indexOfOriginal = originalLength;//原始字符串的下标int indexOfNew = newLength;//新的字符串的下标while (indexOfOriginal >= 0 && indexOfNew > indexOfOriginal){if (string[indexOfOriginal] == ' '){string[indexOfNew--] = '0';//这里是先赋值,然后-1,下同string[indexOfNew--] = '2';string[indexOfNew--] = '%';}else{string[indexOfNew--] = string[indexOfOriginal];}--indexOfOriginal;}
}void Test(char* testName, char string[], int length, char expected[])
{if (testName != NULL)printf("%s begins: ", testName);ReplaceBlank(string, length);if (expected == NULL && string == NULL)printf("passed.\n");else if (expected == NULL && string != NULL)printf("failed.\n");else if (strcmp(string, expected) == 0) {cout << "string=" << string << endl;cout << "expected=" << expected << endl;printf("passed.\n");}else{cout << "string=" << string << endl;cout << "expected=" << expected << endl;printf("failed.\n");}
}// 空格在句子中间
void Test1()
{const int length = 100;char string[length] = "hello world";Test("Test1", string, length, "hello%20world");
}// 空格在句子开头
void Test2()
{const int length = 100;char string[length] = " helloworld";Test("Test2", string, length, "%20helloworld");
}// 空格在句子末尾
void Test3()
{const int length = 100;char string[length] = "helloworld ";Test("Test3", string, length, "helloworld%20");
}// 连续有两个空格
void Test4()
{const int length = 100;char string[length] = "hello  world";Test("Test4", string, length, "hello%20%20world");
}// 传入NULL
void Test5()
{Test("Test5", NULL, 0, NULL);
}// 传入内容为空的字符串
void Test6()
{const int length = 100;char string[length] = "";Test("Test6", string, length, "");
}//传入内容为一个空格的字符串
void Test7()
{const int length = 100;char string[length] = " ";Test("Test7", string, length, "%20");
}// 传入的字符串没有空格
void Test8()
{const int length = 100;char string[length] = "helloworld";Test("Test8", string, length, "helloworld");
}// 传入的字符串全是空格
void Test9()
{const int length = 100;char string[length] = "   ";Test("Test9", string, length, "%20%20%20");
}int main()
{Test1();Test2();Test3();Test4();Test5();Test6();Test7();Test8();Test9();cin.get();cin.get();return 0;
}

04_ReplaceBlank相关推荐

  1. 替换空格(C++和Python 实现)

    (说明:本博客中的题目.题目详细说明及参考代码均摘自 "何海涛<剑指Offer:名企面试官精讲典型编程题>2012年") 题目 请实现一个函数,把字符串中的每个空格替换 ...

最新文章

  1. php 生成等比例缩略图,PHP等比例生成缩略图
  2. Xen虚拟化之一:Xen环境组件详解
  3. py2neo 基本用法
  4. CentOS7.5 使用二进制程序部署Kubernetes1.12.2(三)
  5. python 回溯法 子集树模板 系列 —— 1、8 皇后问题
  6. 程序发出的广播其他程序收不到_RabbitMQ 如何实现对同一个应用的多个节点进行广播...
  7. JS中数组和字符串具有的方法,以及substring,substr和slice的用法与区别
  8. python map lambda 分割字符串_Python特殊语法:filter、map、reduce、lambda [转]
  9. STL不是线程安全的啊
  10. Axure高保真移动端电商app通用模板、axure高保真移动端教育app通用模板、旅游app通用模板、电商app、教育app、旅游app 、直播、在线教育、旅游、Axure原型、rp原型
  11. mysql插入日期_MySQL 的两个特殊数据类型属性 unsigned与 zerofill
  12. x86体系Linux内核进程切换原理(64位和32位)
  13. c语言实现简易图书管理系统
  14. 丁香园 (http://www.dxy.cn)这个名字听着
  15. 楪祈机器人_饥荒 Inori楪祈人物MOD
  16. bin和cue怎么合并_bin和cue格式的文件怎么用?
  17. MongoDB——基础篇(文档操作)
  18. matlab 安装coder工具包,matlab coder 工具箱使用教程
  19. Android中Callable、Future、FutureTask的概念以及几种线程池的使用
  20. 【CSS】CSS盒子模型

热门文章

  1. 婚姻就像一双鞋,合不合适只有脚知道
  2. 图片image和byte处理,fileupload上传图片
  3. CG-CTF-Web-COOKIE
  4. linux 自动保存网页,Linux 定时备份网站数据至七牛云存储
  5. WAMPSerrver集成环境的下载安装
  6. 使用HTML5 details,summary实现,展开,下拉,树的效果
  7. Photoshop扣除特定颜色背景
  8. AngularJS(6)-选择框Select
  9. Android Note - 内存优化
  10. containerd项目正式从CNCF毕业