剑指offer面试题:替换空格

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

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


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







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

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


运行结果:

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.

请按任意键继续. . .


猜你喜欢

转载自blog.csdn.net/sinat_35297665/article/details/80568835