https://blog.csdn.net/yanxiaolx/article/details/52235212
?題目:請實現一個函數,把字符串中的每個空格替換成“%20”。例如輸入“We are happy.”,則輸出“We%20are%20happy.”。
解析:時間復雜度為O(n)的解法。
完整代碼及測試用例實現:
- #include<iostream>??
- using?namespace?std;??
- #include?<cstring>??
- ??
- //length?為字符數組string的總容量??
- void?ReplaceBlank(char?string[],?int?length)??
- {??
- ????if?(string?==?NULL&&length?<=?0)??
- ????{??
- ????????return;??
- ????}??
- ????//reallyLength?為字符串string的實際長度??
- ????int?reallyLength?=?0,?numberOfBlank?=?0,i=0;??
- ??????
- ????while?(string[i]!='\0')??
- ????{??
- ????????++reallyLength;??
- ??
- ????????if?(string[i]?==?'?')??
- ????????{??
- ????????????++numberOfBlank;??
- ????????}??
- ????????++i;??
- ????}??
- ??
- ????//newLength?為把空格替換成'%20'之后的長度??
- ????int?newLength?=?reallyLength?+?numberOfBlank?*?2;??
- ????if?(newLength?>?length)??
- ????{??
- ????????return;??
- ????}??
- ??
- ????int?indexOfReally?=?reallyLength;??
- ????int?indexOfNew?=?newLength;??
- ????while?(indexOfReally?>=?0?&&?indexOfNew?>indexOfReally)??
- ????{??
- ????????if?(string[indexOfReally]?==?'?')??
- ????????{??
- ????????????string[indexOfNew--]?=?'0';??
- ????????????string[indexOfNew--]?=?'2';??
- ????????????string[indexOfNew--]?=?'%';??
- ????????}??
- ????????else??
- ????????{??
- ????????????string[indexOfNew--]?=?string[indexOfReally];??
- ????????}??
- ??
- ????????--indexOfReally;??
- ????}??
- }??
- ??
- ??
- //?====================測試代碼====================??
- void?Test(char*?testName,?char?string[],?int?length,?char?expected[])??
- {??
- ????if?(testName?!=?NULL)??
- ????{??
- ????????cout?<<?testName?<<?"?begins:?";??
- ????}??
- ??
- ????ReplaceBlank(string,?length);??
- ??
- ????if?(expected?==?NULL?&&?string?==?NULL)??
- ????{??
- ????????cout?<<?"passed."?<<?endl;??
- ????}??
- ????else?if?(expected?==?NULL?&&?string?!=?NULL)??
- ????{??
- ????????cout?<<?"failed."?<<?endl;??
- ????}??
- ????else?if?(strcmp(string,?expected)?==?0)??
- ????{??
- ????????cout?<<?"passed."?<<?endl;??
- ????}??
- ????else??
- ????{??
- ????????cout?<<?"failed."?<<?endl;??
- ????}??
- }??
- ??
- ??
- void?Test1()??
- {??
- ????//?空格在句子中間??
- ????const?int?length?=?100;??
- ??
- ????char?string[length]?=?"we?are?happy.";??
- ????Test("Test1",?string,?length,?"we%20are%20happy.");??
- }??
- ??
- ??
- void?Test2()??
- {??
- ????//?空格在句子開頭??
- ????const?int?length?=?100;??
- ??
- ????char?string[length]?=?"?wearehappy.";??
- ????Test("Test2",?string,?length,?"%20wearehappy.");??
- }??
- ??
- void?Test3()??
- {??
- ????//?空格在句子末尾??
- ????const?int?length?=?100;??
- ??
- ????char?string[length]?=?"wearehappy.?";??
- ????Test("Test3",?string,?length,?"wearehappy.%20");??
- }??
- ??
- void?Test4()??
- {??
- ????//?連續有兩個空格??
- ????const?int?length?=?100;??
- ??
- ????char?string[length]?=?"we??are?happy.";??
- ????Test("Test4",?string,?length,?"we%20%20are%20happy.");??
- }??
- ??
- void?Test5()??
- {??
- ????//?傳入NULL??
- ????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]?=?"wearehappy.";??
- ????Test("Test8",?string,?length,?"wearehappy.");??
- }??
- ??
- 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();??
- ??
- ????system("pause");???
- ????return?0;??
- }??
運行結果:
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.
請按任意鍵繼續. . .