Flip word order, flip word
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: first flip the entire sentence by character, and then rotate the characters of each word.
1 # include <string> 2 # include "stdafx. h "3 4 void Reverse (char * pBegin, char * pEnd) 5 {6 if (pBegin = NULL | pEnd = NULL) 7 return; 8 9 while (pBegin <pEnd) 10 {11 char temp = * pBegin; 12 * pBegin = * pEnd; 13 * pEnd = temp; 14 15 pBegin ++, pEnd --; 16} 17} 18 19 char * ReverseSentence (char * pData) 20 {21 if (pData = NULL) 22 return NULL; 23 24 char * pBegin = pData; 25 26 char * pEnd = pData; 27 while (* pEnd! = '\ 0') 28 pEnd ++; 29 pEnd --; 30 31 // flip the entire sentence 32 Reverse (pBegin, pEnd ); 33 34 // In the flipped sentence, each word 35 pBegin = pEnd = pData; 36 while (* pBegin! = '\ 0') 37 {38 if (* pBegin = '') 39 {40 pBegin ++; 41 pEnd ++; 42} 43 else if (* pEnd = ''| * pEnd = '\ 0') 44 {45 Reverse (pBegin, -- pEnd ); 46 pBegin =++ pEnd; 47} 48 else49 {50 pEnd ++; 51} 52} 53 54 return pData; 55} 56 57 58 int main () 59 {60 char input [] = "I am a student. "; 61 printf (" % s \ n ", input); 62 printf (" After reverse. \ n "); 63 ReverseSentence (input); 64 printf (" % s \ n ", input); 65 66 return 0; 67}