Flip word order, flip word

Source: Internet
Author: User

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}

Related Article

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.