// 面试题58(一):翻转单词顺序 // 题目:输入一个英文句子,翻转句子中单词的顺序,但单词内字符的顺序不变。 // 为简单起见,标点符号和普通字母一样处理。例如输入字符串"I am a student. ", // 则输出"student. a am I"。 #include <cstdio> #include "StringUtil.h" #include <string> char* ReverseSentence(char* pData) { if (pData == nullptr) return nullptr; char* pBegin = pData; char* pEnd = pData; //找字符串结尾 while (*pEnd != '\0') ++pEnd; --pEnd; Reverse(pBegin, pEnd); //第一次反转 pBegin = pEnd = pData; while (*pBegin != '\0') { if (*pBegin == ' ') //此时已经完成一个单词的反转, pBegin = pEnd { ++pBegin; ++pEnd; } else if (*pEnd == ' ' || *pEnd == '\0') //找到单词末尾 { Reverse(pBegin, --pEnd); //反转单词, 注意不是引用, 因此pBegin和pEnd值不会改变 pBegin = ++pEnd; } else ++pEnd; } return pData; }
// ====================测试代码==================== void Test(const char* testName, char* input, const char* expectedResult) { if (testName != nullptr) printf("%s begins: ", testName); ReverseSentence(input); if ((input == nullptr && expectedResult == nullptr) || (input != nullptr && strcmp(input, expectedResult) == 0)) printf("Passed.\n\n"); else printf("Failed.\n\n"); } // 功能测试,句子中有多个单词 void Test1() { char input[] = "I am a student."; char expected[] = "student. a am I"; Test("Test1", input, expected); } // 功能测试,句子中只有一个单词 void Test2() { char input[] = "Wonderful"; char expected[] = "Wonderful"; Test("Test2", input, expected); } // 鲁棒性测试 void Test3() { Test("Test3", nullptr, nullptr); } // 边界值测试,测试空字符串 void Test4() { char input[] = ""; char expected[] = ""; Test("Test4", input, expected); } // 边界值测试,字符串中只有空格 void Test5() { char input[] = " "; char expected[] = " "; Test("Test5", input, expected); } int main(int argc, char* argv[]) { Test1(); Test2(); Test3(); Test4(); Test5(); return 0; }
分析:不是引用传递不会改变值。