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