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