翻转句子中的单词顺序,输入一个句子,翻转句子中的单词顺序,但是单个单词中的字母顺序不变,如“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;
}