<OFFER05> 05_ReplaceSpaces替换空格
/******************************************************************* Copyright(c) 2016, Harry He All rights reserved. Distributed under the BSD license. (See accompanying file LICENSE.txt at https://github.com/zhedahht/CodingInterviewChinese2/blob/master/LICENSE.txt) *******************************************************************/ //================================================================== // 《剑指Offer——名企面试官精讲典型编程题》代码 // 作者:何海涛 //================================================================== // 面试题5:替换空格 // 题目:请实现一个函数,把字符串中的每个空格替换成"%20"。例如输入“We are happy.”, // 则输出“We%20are%20happy.”。 #include <cstdio> #include <cstring> #if 0 /*length 为字符数组str的总容量,大于或等于字符串str的实际长度*/ void ReplaceBlank(char str[], int length) { if (str == nullptr && length <= 0) return; /*originalLength 为字符串str的实际长度*/ int originalLength = 0; int numberOfBlank = 0; int i = 0; while (str[i] != '\0') { ++originalLength; if (str[i] == ' ') ++numberOfBlank; ++i; } /*newLength 为把空格替换成'%20'之后的长度*/ int newLength = originalLength + numberOfBlank * 2; if (newLength > length) return; int indexOfOriginal = originalLength; int indexOfNew = newLength; while (indexOfOriginal >= 0 && indexOfNew > indexOfOriginal) { if (str[indexOfOriginal] == ' ') { str[indexOfNew--] = '0'; str[indexOfNew--] = '2'; str[indexOfNew--] = '%'; } else { str[indexOfNew--] = str[indexOfOriginal]; } --indexOfOriginal; } } #endif #if 1 /*length 为字符数组str的总容量,大于或等于字符串str的实际长度*/ void ReplaceBlank(char str[], int length) { if ((str == nullptr) || (length < 0)) return; //在原来字符串上进行替换,length保证开够了空间 int length_of_origin = 0; int length_of_blank = 0; int i = 0; //如何知道字符串的长度 while (str[i] != '\0') { if (str[i] == ' ') { length_of_blank++; } length_of_origin++; i++; } int length_new = length_of_origin + 2 * length_of_blank; int p1 = length_of_origin; int p2 = length_new; while (p2 > p1) { if (str[p1] != ' ') { str[p2] = str[p1]; p2--; p1--; } else { p1--; str[p2--] = '0'; str[p2--] = '2'; str[p2--] = '%'; } } } #endif // ====================测试代码==================== void Test(char* testName, char str[], int length, char expected[]) { if (testName != nullptr) printf("%s begins: ", testName); ReplaceBlank(str, length); if (expected == nullptr && str == nullptr) printf("passed.\n"); else if (expected == nullptr && str != nullptr) printf("failed.\n"); else if (strcmp(str, expected) == 0) printf("passed.\n"); else printf("failed.\n"); } // 空格在句子中间 void Test1() { const int length = 100; char str[length] = "hello world"; Test("Test1", str, length, "hello%20world"); } // 空格在句子开头 void Test2() { const int length = 100; char str[length] = " helloworld"; Test("Test2", str, length, "%20helloworld"); } // 空格在句子末尾 void Test3() { const int length = 100; char str[length] = "helloworld "; Test("Test3", str, length, "helloworld%20"); } // 连续有两个空格 void Test4() { const int length = 100; char str[length] = "hello world"; Test("Test4", str, length, "hello%20%20world"); } // 传入nullptr void Test5() { Test("Test5", nullptr, 0, nullptr); } // 传入内容为空的字符串 void Test6() { const int length = 100; char str[length] = ""; Test("Test6", str, length, ""); } //传入内容为一个空格的字符串 void Test7() { const int length = 100; char str[length] = " "; Test("Test7", str, length, "%20"); } // 传入的字符串没有空格 void Test8() { const int length = 100; char str[length] = "helloworld"; Test("Test8", str, length, "helloworld"); } // 传入的字符串全是空格 void Test9() { const int length = 100; char str[length] = " "; Test("Test9", str, length, "%20%20%20"); } int main(int argc, char* argv[]) { Test1(); Test2(); Test3(); Test4(); Test5(); Test6(); Test7(); Test8(); Test9(); return 0; }
标签:
OFFER
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix