All the content comes from the "sword Point offer".
Topic One:
Enter an English sentence, flipping the order of the words in the sentence, but the order of the characters in the word does not change. For simplicity, punctuation is treated like normal characters. For example, the input string "I am a student.", the output
"Student. A am I ".
ANSWER:
voidReverseChar*pbegin,Char*pEnd) { if(Pbegin = = NULL | | pEnd = =NULL)return; while(Pbegin <pEnd) { CharTMP = *Pbegin; *pbegin = *pEnd; *pend =tmp; Pbegin++; PEnd--; }}Char* Reversesentence (Char*pDate) { if(PDate = =NULL)returnNULL; Char*pbegin =pDate; Char*pend =pDate; while(*pend! =' /') PEnd++; PEnd--; Reverse (Pbegin, pEnd); Pbegin= PEnd =pDate; while(*pbegin! =' /') { if(*pbegin = =' ') {Pbegin++; PEnd++; } Else if(*pend = =' '|| *pend = =' /') {reverse (Pbegin,--pEnd); Pbegin= ++pEnd; } ElsepEnd++; } returnpDate;}
Topic Two:
The left rotation of a string is the transfer of several characters in front of the string to the end of the string. Define a function that implements the function of a string left rotation operation. For example, the input string "ABCDEFG" and the number 2, the function will return the left rotation of 2 bits to get the result "cdefgab".
ANSWER:
Char* Leftrotatestring (Char*PSTR,intN) { if(PStr! =NULL) { intNlength = static_cast<int>(strlen (PSTR)); if(Nlength >0&& n >0&& N <nlength) { Char*pfirststart =pStr; Char*pfirstend = Pfirststart + N-1; Char*psecondstart = Pfirststart +N; Char*psecondend = pStr + nlength-1; Reverse (Pfirststart, pfirstend); Reverse (Psecondstart, psecondend); Reverse (Pfirststart, psecondend); } } returnpStr;}
There are also corresponding topics on the Leetcode.
https://leetcode.com/problems/reverse-words-in-a-string/
Flip word order VS left spin string