Solving notes (8) -- turning the order of words in a sentence

Source: Internet
Author: User

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

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.