Problem description: enter an English sentence to flip the order of words in the sentence, but the order of Characters in the word remains unchanged. Words in a sentence are separated by space characters. For simplicity, punctuation marks are processed like normal letters. For example, if "I am a student." Is input, "student. A am I" is output ".
Thought: first, the entire sentence is reversed, and then scanning starts from the first character. Each time a word is scanned (blank or ending character is encountered), the word is reversed.
Code: // left-closed and right-open
Void reverse (char * pbegin, char * pend) {If (pbegin = pend) return; while (pbegin <-- pend) {char TMP = * pbegin; * pbegin = * pend; * pend = TMP; pbegin ++;} void reversesentence (char * psentence) {If (psentence = NULL) return; reverse (psentence, psentence + strlen (psentence); char * pbegin = psentence; char * pend = psentence; while (* pend! = '\ 0') {If (* pend! = '') Pend ++; else {// when a space is encountered, reverse the word reverse (pbegin, pend); pbegin =++ pend ;}} // reverse the last word reverse (pbegin, pend );}
I enjoy the copyright of blog articles, reprint please indicate the source http://blog.csdn.net/wuzhekai1985