Jz-c-42

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.