https://blog.csdn.net/yanxiaolx/article/details/52235212

题目:请实现一个函数,把字符串中的每个空格替换成“%20”。例如输入“We are happy.”,则输出“We%20are%20happy.”。

解析:时间复杂度为O(n)的解法。

完整代码及测试用例实现:

[cpp] view plaincopy
  1. #include<iostream>
  2. using namespace std;
  3. #include <cstring>
  4. //length 为字符数组string的总容量
  5. void ReplaceBlank(char string[], int length)
  6. {
  7. if (string == NULL&&length <= 0)
  8. {
  9. return;
  10. }
  11. //reallyLength 为字符串string的实际长度
  12. int reallyLength = 0, numberOfBlank = 0,i=0;
  13. while (string[i]!='\0')
  14. {
  15. ++reallyLength;
  16. if (string[i] == ' ')
  17. {
  18. ++numberOfBlank;
  19. }
  20. ++i;
  21. }
  22. //newLength 为把空格替换成'%20'之后的长度
  23. int newLength = reallyLength + numberOfBlank * 2;
  24. if (newLength > length)
  25. {
  26. return;
  27. }
  28. int indexOfReally = reallyLength;
  29. int indexOfNew = newLength;
  30. while (indexOfReally >= 0 && indexOfNew >indexOfReally)
  31. {
  32. if (string[indexOfReally] == ' ')
  33. {
  34. string[indexOfNew--] = '0';
  35. string[indexOfNew--] = '2';
  36. string[indexOfNew--] = '%';
  37. }
  38. else
  39. {
  40. string[indexOfNew--] = string[indexOfReally];
  41. }
  42. --indexOfReally;
  43. }
  44. }
  45. // ====================测试代码====================
  46. void Test(char* testName, char string[], int length, char expected[])
  47. {
  48. if (testName != NULL)
  49. {
  50. cout << testName << " begins: ";
  51. }
  52. ReplaceBlank(string, length);
  53. if (expected == NULL && string == NULL)
  54. {
  55. cout << "passed." << endl;
  56. }
  57. else if (expected == NULL && string != NULL)
  58. {
  59. cout << "failed." << endl;
  60. }
  61. else if (strcmp(string, expected) == 0)
  62. {
  63. cout << "passed." << endl;
  64. }
  65. else
  66. {
  67. cout << "failed." << endl;
  68. }
  69. }
  70. void Test1()
  71. {
  72. // 空格在句子中间
  73. const int length = 100;
  74. char string[length] = "we are happy.";
  75. Test("Test1", string, length, "we%20are%20happy.");
  76. }
  77. void Test2()
  78. {
  79. // 空格在句子开头
  80. const int length = 100;
  81. char string[length] = " wearehappy.";
  82. Test("Test2", string, length, "%20wearehappy.");
  83. }
  84. void Test3()
  85. {
  86. // 空格在句子末尾
  87. const int length = 100;
  88. char string[length] = "wearehappy. ";
  89. Test("Test3", string, length, "wearehappy.%20");
  90. }
  91. void Test4()
  92. {
  93. // 连续有两个空格
  94. const int length = 100;
  95. char string[length] = "we  are happy.";
  96. Test("Test4", string, length, "we%20%20are%20happy.");
  97. }
  98. void Test5()
  99. {
  100. // 传入NULL
  101. Test("Test5", NULL, 0, NULL);
  102. }
  103. void Test6()
  104. {
  105. // 传入内容为空的字符串
  106. const int length = 100;
  107. char string[length] = "";
  108. Test("Test6", string, length, "");
  109. }
  110. void Test7()
  111. {
  112. //传入内容为一个空格的字符串
  113. const int length = 100;
  114. char string[length] = " ";
  115. Test("Test7", string, length, "%20");
  116. }
  117. void Test8()
  118. {
  119. // 传入的字符串没有空格
  120. const int length = 100;
  121. char string[length] = "wearehappy.";
  122. Test("Test8", string, length, "wearehappy.");
  123. }
  124. void Test9()
  125. {
  126. // 传入的字符串全是空格
  127. const int length = 100;
  128. char string[length] = "   ";
  129. Test("Test9", string, length, "%20%20%20");
  130. }
  131. int main()
  132. {
  133. Test1();
  134. Test2();
  135. Test3();
  136. Test4();
  137. Test5();
  138. Test6();
  139. Test7();
  140. Test8();
  141. Test9();
  142. system("pause");
  143. return 0;
  144. }

运行结果:

Test1 begins: passed.

Test2 begins: passed.

Test3 begins: passed.

Test4 begins: passed.

Test5 begins: passed.

Test6 begins: passed.

Test7 begins: passed.

Test8 begins: passed.

Test9 begins: passed.

请按任意键继续. . .

剑指offer面试题:替换空格相关推荐

  1. 剑指Offer(48)-[ArrayString]替换空格

    点击查看剑指Offer全解[Java & Golang]实现 题目描述 请实现一个函数,将一个字符串中的每个空格替换成"%20".例如,当字符串为We Are Happy. ...

  2. 剑指Offer(二):替换空格

    参考链接: https://cuijiahua.com/blog/2017/11/basis_2.html https://blog.csdn.net/wang454592297/article/de ...

  3. Java之《剑指Offer》:字符串替换空格,请实现一个函数,将一个字符串中的每个空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happ

    1. 题目描述 请实现一个函数,将一个字符串中的每个空格替换成"%20".例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy. ① ...

  4. 剑指offer面试题[4]-空格替换

    题目描述 请实现一个函数,将一个字符串中的空格替换成"%20".例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy. 以输入hell ...

  5. 剑指offer二之替换空格

    一.题目: 请实现一个函数,将一个字符串中的空格替换成"%20".例如,当字符串为I love you.则经过替换之后的字符串为I%20love%20You. 二.解题方法: 方法 ...

  6. 【剑指offer】_02替换空格

    题目描述 请实现一个函数,将一个字符串中的每个空格替换成"%20".例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy. 解题思路 ...

  7. python剑指offer替换空格_《剑指Offer》字符串 替换空格

    //str.replace(char oldChar,char newChar);用字符newChar替换oldChar;返回一个新的字符串 public class Solution { publi ...

  8. 剑指offer二:替换空格

    题目描述: 请实现一个函数,将一个字符串中的空格替换成"%20".例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy. 解题思路: ...

  9. 【剑指Offer面试题】 九度OJ1510:替换空格

    c/c++ 中的字符串以"\0"作为结尾符.这样每一个字符串都有一个额外字符的开销. 以下代码将造成内存越界. char str[10]; strcpy(str, "01 ...

  10. 剑指offer——面试题4:替换空格

    #剑指offer--面试题4:替换空格 此题的关键思路在于字符串的从后向前复制!!! class Solution { public:void replaceSpace(char *str,int l ...

最新文章

  1. 【Android UI】图片 + 文字展示by SpannableStringBuilder
  2. 出块过程 (1)close发送消息
  3. 构造数列中的常见变形总结【中阶和高阶辅导】
  4. java枚举怎么编译不行的_java枚举类型
  5. video-js RTMP直播
  6. 【kafka】kerberos client is being asked for a password not available to garner authentication informa
  7. AndroidStuido连接不上手机的解决方法
  8. java中正则表达式截取字符串
  9. css 多行文本的溢出显示省略号(移动端)
  10. 2016年总结与2017展望
  11. 软考信息系统监理师:2016年4月22日作业
  12. Excel打印时完整显示合并的单元格区域
  13. linux下用户名怎么修改密码,LINUX用户名密码忘记怎么修改用户密码
  14. 数据之美:迄今 10 佳数据可视化示例
  15. 微信公众号开发----生成带参数的临时二维码
  16. python 爬取知网url
  17. 中国大学MOOC大学生心理健康试题及答案
  18. 全球首家BAYC NFT主题餐厅BoredHungry开业,可使用APE和ETH支付
  19. Ubuntu安装开源终端工具Tabby
  20. 线上服务器CPU负载过高的问题解决过程

热门文章

  1. 最全Pycharm教程(10)——Pycharm调试器总篇
  2. JDBC连接(MySql)数据库步骤,以及查询、插入、删除、更新等十一个处理数据库信息的功能。...
  3. poj 1862 Stripies/优先队列
  4. commons-lang的FastDateFormat性能测试
  5. 三菱st语言编程实例_LD、FBD、IL、ST、SFC、CFC六种编程语言的特点
  6. 大学物理质点动力学思维导图_生理学 | 思维导图
  7. java对hashmap迭代_Java:通过HashMap迭代,这样更有效率?
  8. php copy 文件夹,php删除与复制文件夹及其文件夹下所有文件的实现代码
  9. php 删除单个文件大小,php删除指定大小的jpg文件
  10. cass展点不在原位置,cass中打开一副图后,通过绘图处理-——展高程点,怎么之前的图形就不显示了,,只剩下高程点!!...