[PAT B1009] ironic

[PAT B1009] ironic

Given a word of English, asking you to write a program, all the words of the sentence order reversed output.
Input format: test input comprising a test case, given the string length does not exceed a total of 80 in a row. String composed of several words and a number of spaces, where the word is English letters (case is case) consisting of string, separated by a space between words, the input end of the sentence to ensure that no extra space.
Output format: one row for each test case output, output sentence after the reverse.
Sample input:
the Hello World Here Come the I
sample output:
Come the I Here the Hello World

#include <stdio.h>
#include <cstring>

//用二维数组解决
void split(char *str) {
    char ans[90][90];
    int length = strlen(str);
    int r = 0, c = 0;
    //Hello World Here I Come
    for (int i = 0; i < length; i++) {

        if (str[i] != ' ') {
            ans[r][c++] = str[i];

        } else {
            ans[r][c] = '\0';
            r++;
            c = 0;
        }

    }

    for (int i = r; i >= 0; i--) {
        printf("%s", ans[i]);
        if (i > 0)
            printf(" ");//控制最后一个空格
    }

}

int main() {
    char str[80];
    while (gets(str)) {
        split(str);
        printf("\n");
    }

}

Test Results:
Here Insert Picture Description

Published 33 original articles · won praise 1 · views 4150

Guess you like

Origin blog.csdn.net/qq_39827677/article/details/103855278