"Sword Point Offer": [42] Reverse the order of words in English

Source: Internet
Author: User

title: Enter an English sentence, flipping the order of the words in the sentence, but the order of the characters within the word is unchanged. For simplicity, punctuation is treated like ordinary letters.

Operation on string, main problem: Be sure to note that the string is null and the access is out of bounds! And then find ' you '.

For example, the input string; "I love you.", after flipping it into: "You." Love I ". Do you see it, intentionally, (no matter the grammar) hehe!

Scenario One: Two-time rollover method. The two-time flip method is to flip the entire string first: ". Uoy evol I"; the second flip flips the individual words in the sentence: "You." Love I ". This will be a success! The key point of this scheme is to implement a string rollover.
Specific implementation code:
#include <iostream>using namespace Std;char strr[]= "Jenny, I Love you."; void Reverse (char *pbegin,char *pend) {if (Null==pbegin | | Null==pend) Return;while (pbegin<pend) {char temp=*pend;*pend=*pbegin;*pbegin=temp;pbegin++;p end--;}} char * reversesentence (char *str) {if (NULL==STR) return Null;char *pbegin=str;char *pend=str;while (*pend!= ' n ') pEnd++; pend--;//First step: Flip the whole sentence first; Reverse (pbegin,pend);//Second step: Flip each word in the sentence, Pbegin=pend=str;while (*pbegin!= ') {if (*pbegin== ') ') {pbegin++;p end++;} else if (*pend== ' | | *pend== ') {Reverse (pbegin,--pEnd);p begin=++pend;} elsepend++;} return str;} int main () {char *pp=null;cout<< "is not flipped before:" <<strr<<endl;pp=reversesentence (STRR);cout<< "flipped:" <<pp<<endl;system ("pause"); return 0;}

Operation Result:



"Sword Point Offer": [42] Reverse the order of words in English

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.