11275 - Sentence Reversal   

Description

In this problem, you are given several sentences, and you have to print the reversal of each of them.

A reversal of a sentence is the reversed order of words in the original sentence. For example, for the input string "this is a book", the result should be "book a is this".

Hint 1: we suggest you to do the task by a recursive function. The function seeks the first word from current location, and call itself recursively. Before each return, the function will print the word it found. Then all words will be printed in the reversed order.

The function prototype is something like this:

    void sentence_reversal( char *now ) ;    // which seek the first word from *now, and pass an new *now location to the called function

And the recursion is like the following figure:

this       (4th printed word)
    -> is      (3rd printed word)
        -> a       (2nd printed word)
            -> book  (1st printed word)

Hint 2: Print the spaces between words, and there should be no blanks (spaces) at the begin or the end of new sentences. If the original sentence has only one word, print the word directly without any spaces.

Note: Another method to solve this problem is seeking words from the end of the original sentence. But we strongly recommend you to try the recursive way as a practice.

Hint 3: Use fgets(). (https://www.dummies.com/programming/c/how-to-use-the-fgets-function-for-text-input-in-c-programming/)

Input

There are several lines of input, and each line presents a sentence.

No lines are longer than 500 characters, and there will be no empty lines. At most 10 sentences will appear in a test case.

Words in sentences are separated by a space (' '). There will be no continuous spaces, or any spaces at the beginning or end of the sentences.

Output

Print out each reversal sentence. All words in a reversal sentence are separated by a space, and there is a '\n' at the end of each line.

Sample Input  Download

Sample Output  Download

Tags




Discuss