Sword Point 42nd: Flip the word order: Enter an English sentence, flip the order of the words in the sentence, but the order of the characters in the word does not change
1 //============================================================================2 //Name:jz-c-42.cpp3 //Author:laughing_lz4 //Version:5 //Copyright:all Right Reserved6 //Description: Flip the word order: Enter an English sentence, flip the order of the words in the sentence, but the order of the characters in the word does not change7 //============================================================================8 9#include <iostream>Ten#include <stdio.h> One#include"StringUtil.h" A#include <string.h> - using namespacestd; - the Char* Reversesentence (Char*pData) { - if(PData = =NULL) - returnNULL; - + Char*pbegin =PData; - + Char*pend =PData; A while(*pend! =' /') atpend++; -pend--; - - //Flip the whole sentence - Reverse (Pbegin, pEnd); - in //Flip each word in a sentence -Pbegin = PEnd =PData; to while(*pbegin! =' /') { + if(*pbegin = =' ') { -pbegin++; thepend++; *}Else if(*pend = =' '|| *pend = =' /') { $Reverse (Pbegin,--pEnd);Panax NotoginsengPbegin = + +pEnd; -}Else { thepend++; + } A } the + returnPData; - } $ $ //==================== test Code ==================== - voidTest (Char* TestName,Char* Input,Char*Expectedresult) { - if(TestName! =NULL) theprintf"%s begins:", testname); - Wuyi reversesentence (input); the - if(Input = = NULL && Expectedresult = =NULL) Wu|| (input = NULL && strcmp (input, expectedresult) = =0)) -printf"passed.\n\n"); About Else $printf"failed.\n\n"); - } - - //functional testing, with multiple words in a sentence A voidTest1 () { + CharInput[] ="I am a student."; the CharExpected[] ="student. A AM I"; - $Test ("Test1", input, expected); the } the the //functional test with only one word in the sentence the voidTest2 () { - CharInput[] ="Wonderful"; in CharExpected[] ="Wonderful"; the theTest ("Test2", input, expected); About } the the //Robustness Testing the voidTest3 () { +Test ("Test3", NULL, NULL); - } the Bayi //boundary value test, test empty string the voidTest4 () { theTest ("Test4","",""); - } - the //boundary value test with only spaces in the string the voidTest5 () { the CharInput[] =" "; the CharExpected[] =" "; -Test ("Test5", input, expected); the } the the intMainintargcChar**argv) {94 Test1 (); the Test2 (); the Test3 (); the Test4 ();98 Test5 (); About - return 0;101}
Jz-c-42