PAT 10-0 speak the opposite, pat10-0 speak the opposite

Source: Internet
Author: User

PAT 10-0 speak the opposite, pat10-0 speak the opposite

I have written two implementation methods, the second of which is referring to Yomman yuanyou (http://www.cnblogs.com/yomman/p/4271949.html ). My method (method 1) is to use an array to store the input string, and the other array to store the first address of each word in the string, then ······; method 2: Using OJ will automatically add a file terminator at the end of the input. I did not expect it, and it cannot be implemented in my own compiler (Dev-C ++, but it is indeed a lot more concise, there are some minor faults, a little waste of space. The question setting requirements and code implementation are as follows. method 1 is commented out.

/ *
    Name:
    Copyright:
    Author:
    Date: 03/04/15 07:59
    Description:
Given a sentence of English, you are asked to write a program that outputs the order of all the words in the sentence in reverse order.

Input format: The test input contains a test case, which gives a string with a total length of 80 in one line. The string is composed of several words and spaces. The word is a string of English letters (case sensitive). The words are separated by 1 space. Enter to ensure that there are no extra spaces at the end of the sentence.

Output format: The output of each test case occupies one line, and the sentences in reverse order are output.

Input example:
Hello World Here I Come
Sample output:
Come I Here World Hello
* /

#include <stdio.h>
// # include <string.h>
// # include <stdbool.h>

#define MAX 80

// refering to another's
int main ()
{
// freopen ("in.txt", "r", stdin); // for test
    char s [MAX / 2] [MAX + 1];
    int cnt;
    
    cnt = 0;
    while (scanf ("% s", s [cnt ++])! = EOF);
    
    cnt-= 2;
    while (cnt)
        printf ("% s", s [cnt--]);
    printf ("% s \ n", s [cnt]);
// fclose (stdin); // for test
    return 0;
}

/ *
void inverse (char * s, int l);

int main ()
{
// freopen ("in.txt", "r", stdin); // for test
    char s [MAX + 1];
    int l;
    
    gets (s);
    l = strlen (s);
    
    inverse (s, l);
// fclose (stdin); // for test
    return 0;
}

void inverse (char * s, int l)
{
    char index [l / 2 + 1];
    int i, cnt, tmp;
    bool head;
    
    cnt = 0;
    head = true;
    for (i = 0; i <l; i ++)
    {
        if (s [i]! = '')
        {
            if (head)
            {
                index [cnt ++] = i;
                head = false;
            }
        }
        else
            head = true;
    }
    do
    {
        tmp = index [-cnt];
        while (s [tmp]! = '' && s [tmp])
            putchar (s [tmp ++]);
        if (cnt)
            printf ("");
    } while (cnt);
    printf ("\ n");
}
* /
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.