question No.
Flips the order of the words in the sentence.
Title : Enter an English sentence, flipping the order of the words in the sentence, but the order of the characters within the word is unchanged.
Words are separated by a null character in a sentence. For simplicity, punctuation is treated like ordinary letters.
For example , enter "I am a student." , the output "student. A am I ".
Idea: Start by flipping the entire sentence by character, then flipping each word in the sentence by character
#include <cstring>#include<iostream>using namespacestd;//Swap LettersvoidSwap_word (Char&a,Char&b) { Chartemp; Temp=A; A=b; b=temp;}//Flip the word, here's left,right to save spacevoidReversalword (CharStr[],intLeftintRight ) { //Note that right here is the last one, not the size if(str==null| | Left>right)return; while(left<Right ) {Swap_word (str[left],str[right]); Left++,right--; }}//Flip a sentencevoidReversalsent (Charstr[]) { intlen=strlen (str); //Flip the whole sentence directly, as word processingReversalword (str,0, len-1); intleft=0, right=0;//record the left and right of the word for(intI=0; i<len;i++){ if(str[i]==' '){ //if it is a space, the word range is [left,right-1], where right=iright=i-1; Reversalword (Str,left,right); Left=i+1; } }}intMain () {CharStr[] ="I am a student."; Reversalsent (str); cout<<str<<Endl; return 0;}
Data structure and algorithm surface test questions 80 (10)