[Programming question] the order of words in a flipped sentence

Source: Internet
Author: User

10th question (string)
The order of words in a flipped sentence.
Question: 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 ".

 

Idea: Use the stack to push each word into the stack, and then pop up the output in sequence.

/* 10th question (string) the order of words in the flipped sentence. Question: 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 ". Start time 15: 12end time */# include <iostream> # include <vector> using namespace STD; typedef struct words {int Len; char * start;} words; void reversesentence (char * c) {vector <words> S; // stack of words stored in words W; W. start = C; W. len = 0; For (INT I = 0; C [I]! = '\ 0'; I ++) {If (C [I]! = '') {W. Len ++;} else {If (W. Len! = 0) {S. push_back (w); W. start = C + I + 1; W. len = 0;} else // skip unnecessary spaces {W. start = C + I + 1 ;}} if (W. len! = 0) // press the last word {S. push_back (w);} while (! S. empty () {words W = S. back (); For (INT I = 0; I <W. len; I ++) {cout <* (W. start + I) ;}cout <''; S. pop_back () ;}cout <Endl ;}int main () {char * c = "I am a student. "; reversesentence (c); Return 0 ;}

 

I found that my method is very complicated in space. The common method on the O (n) network is to flip the sentence in the same place and then flip every word back.

Http://www.cnblogs.com/yanhaiming/archive/2012/07/17/2595817.html

# Include <iostream> # include <vector> # include <assert. h ># include <cstring> using namespace STD; void swap (char & A, char & B) {char TMP = B; B = A; A = TMP ;} void swap_str (char * STR, int start, int end) {assert (STR! = NULL & start <= END); int low = start; int high = end; // the entire sentence is flipped by character while (low 

 

[Programming question] the order of words in a flipped sentence

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.