How to flip word order

Source: Internet
Author: User

Topic Description:

Jobdu recently had a new employee fish, who always held an English magazine every morning and wrote some sentences in the book. Colleague Cat was interested in what Fish had written, and one day he borrowed it from fish to read it, but he couldn't understand what it meant. For example, "student." A am I ". It was later realized that the guy had flipped the order of the words in the sentence and the correct sentence would be "I am a student." Cat's flip of one by one is not a good order for these words, can you help him?

Input:

Each test case is one line, representing an English sentence.

We guarantee that the number of words in a sentence will not exceed 600, and the length of each word will not exceed 30. But be aware that fish is a sloppy person, sometimes two words may have many spaces between. For convenience, you can assume that the total number of characters in a line is no more than 50,000, and that punctuation can be handled like normal letters.

This column more highlights: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

Output:

For each test case, output the correct sentence to a single line after the flip.

Sample input:

Student. A AM I

I ' m a freshman and I like jobdu!

Sample output:

I am a student.

jobdu! Like I and freshman a I ' m

Flip the entire string first, and then flip each word.

The code is as follows:

#include <stdio.h> void Swap (char *a,char *b) {Char temp = *a;  
    *a = *b;  
*b = temp;   
        /* Reverse string/void Reverse (char *str,char *begin,char *end) {if (Str==null | | begin==null | | end==null)  
      
    Return  
        while (begin < end) {Swap (begin,end);  
        begin++;  
    end--;  
      
    }/* Reverse word order/void reversesentence (char *str) {if (str = NULL) return;  
    char *begin = str;  
    char *end = str;  
    while (*end!= ' ") end++;  
    end--;  
      
    Reverses the entire string Reverse (Str,begin,end);  
    Reverse each word begin = str;  
    while (*begin = = ") begin++;  
    end = begin;  
        while (*begin!= ' ") {while (*end!= ' && *end!= ') end++;  
        end--;  
      
        Reverse (Str,begin,end);  
        Skip multiple spaces end++;  
     while (*end = = ") end++;   begin = END;  
    int main () {char str[50000];  
        while (gets (str)!= NULL) {reversesentence (str);  
    Puts (str);  
return 0; }

Compared to the next sword refers to the code on the offer, or there is a gap ah! Compared to the code on the book, I feel that I wrote the code is really weak, not simple. Put it on, a little progress, and then come back to make a comparison!

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.