Data structure and algorithm surface test questions 80 (10)

Source: Internet
Author: User

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)

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.