翻轉句子中的單詞順序,輸入一個句子,翻轉句子中的單詞順序,但是單個單詞中的字母順序不變,如“I am a student.”翻轉後爲“student. a am I”。翻轉方式爲,首先將整個句子當成是一個字符串進行翻轉,得到“.tneduts a ma I”。然後,以空格作爲分隔符,將每個單詞分別進行翻轉,得到“student. a am I”,而對於整個句子和單個單詞的翻轉,其翻轉的方式是一樣的
#include <iostream>
using namespace std;
//輸入一個字符串的頭和尾指針,將其中的所有字符進行翻轉
void reversestr(char* strBegin, char* strEnd)
{
while (strBegin < strEnd)
{
char tmpchar;
tmpchar = *strBegin;
*strBegin = *strEnd;
*strEnd = tmpchar;
strBegin++;
strEnd--;
}
}
void ReverseSentence(char* input)
{
if (input==NULL)
{
return;
}
char* strBegin = input;
char* strEnd = input;
while (*strEnd!='\0')
{
strEnd++;
}
strEnd--;
reversestr(strBegin, strEnd); //整個句子作爲一個字符串進行翻轉
while (*strBegin!='\0')
{
strEnd = strBegin;
while (*strEnd != ' ' && *strEnd != '\0')
{
strEnd++;
}
strEnd--;
reversestr(strBegin, strEnd); //翻轉單個單詞
strBegin = ++strEnd;
while (*strBegin==' ')
{
strBegin++;
}
}
}
// ====================測試代碼====================
void Test(char* testName, char* input, char* expectedResult)
{
if (testName != NULL)
printf("%s begins: ", testName);
ReverseSentence(input);
if ((input == NULL && expectedResult == NULL)
|| (input != NULL && 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", NULL, NULL);
}
// 邊界值測試,測試空字符串
void Test4()
{
Test("Test4", "", "");
}
// 邊界值測試,字符串中只有空格
void Test5()
{
char input[] = " ";
char expected[] = " ";
Test("Test5", input, expected);
}
int main(int argc, char* argv[])
{
Test1();
Test2();
Test3();
Test4();
Test5();
return 0;
}
//將字符串中的前面一部分移到字符串的尾部,而將剩下的字符前移
//如“abcdefgh”,旋轉3個字符後爲“defghabc”
#include <iostream>
using namespace std;
char* RotateString(char* input,int n)
{
if (input == NULL )
{
return NULL;
}
if (n == 0)
{
return input;
}
char* FirstPartBegin = input;
char* FirstPartEnd = input + n - 1;
char* SecondPartBegin = input + n;
char* SecondPartEnd = input + n;
while (*SecondPartEnd != '\0')
{
SecondPartEnd++;
}
SecondPartEnd--;
//reversestr函數與前面的一樣
reversestr(FirstPartBegin, FirstPartEnd);
reversestr(SecondPartBegin, SecondPartEnd);
reversestr(FirstPartBegin, SecondPartEnd);
return FirstPartBegin;
}
// ====================測試代碼====================
void Test(char* testName, char* input, int num, char* expectedResult)
{
if (testName != NULL)
printf("%s begins: ", testName);
char* result = RotateString(input, num);
if ((input == NULL && expectedResult == NULL)
|| (input != NULL && strcmp(result, expectedResult) == 0))
printf("Passed.\n\n");
else
printf("Failed.\n\n");
}
// 功能測試
void Test1()
{
char input[] = "abcdefg";
char expected[] = "cdefgab";
Test("Test1", input, 2, expected);
}
// 邊界值測試
void Test2()
{
char input[] = "abcdefg";
char expected[] = "bcdefga";
Test("Test2", input, 1, expected);
}
// 邊界值測試
void Test3()
{
char input[] = "abcdefg";
char expected[] = "gabcdef";
Test("Test3", input, 6, expected);
}
// 魯棒性測試
void Test4()
{
Test("Test4", NULL, 6, NULL);
}
// 魯棒性測試
void Test5()
{
char input[] = "abcdefg";
char expected[] = "abcdefg";
Test("Test5", input, 0, expected);
}
// 魯棒性測試
void Test6()
{
char input[] = "abcdefg";
char expected[] = "abcdefg";
Test("Test6", input, 7, expected);
}
int main(int argc, char* argv[])
{
Test1();
Test2();
Test3();
Test4();
Test5();
Test6();
return 0;
}