pat 1009. 说反话 (20)

给定一句英语,要求你编写程序,将句中所有单词的顺序颠倒输出。

输入格式:测试输入包含一个测试用例,在一行内给出总长度不超过80的字符串。字符串由若干单词和若干空格组成,其中单词是由英文字母(大小写有区分)组成的字符串,单词之间用1个空格分开,输入保证句子末尾没有多余的空格。

输出格式:每个测试用例的输出占一行,输出倒序后的句子。

输入样例:

Hello World Here I Come

输出样例:

Come I Here World Hello
 1 #include <stdio.h>  
 2 int main(void) {  
 3     char array[41][81], c;  
 4     int i = 0, j = 0;  
 5     while ((c = getchar()) != '
') {  
 6         if (c == ' ') {  
 7             array[i][j] = '';  
 8             i++;  
 9             j = 0;  
10         }  
11         else {  
12             array[i][j++] = c;  
13         }  
14     }  
15     array[i][j] = '';  
16     printf("%s", array[i--]);  
17     while (i >= 0) {  
18         printf(" %s", array[i--]);  
19     }  
20     return 0;  
21 }  
View Code
 1 #include <iostream>
 2 #include <string>
 3 using namespace std;
 4  
 5 int main() {
 6     string s;
 7     getline(cin, s);
 8     int len = s.length();
 9     int count = 1;
10     for (int i = 0; i < len; i++) {
11         if (s[i] == ' ')
12             count++;
13     }
14     char **a = new char* [count];
15     for (int i = 0; i < count; i++) {
16         a[i] = new char [len];
17     }
18     
19     int t = 0;
20     for (int i = 0; i < count - 1; i++) {
21         for (int j = 0; j < len; j++) {
22             if (s[t] != ' ') {
23                 a[i][j] = s[t];
24                 t++;
25             }
26             else {
27                 a[i][j] = '';
28                 t++;
29                 break;
30             }
31         }
32     }
33     int temp = t;
34     for (int j = 0; j < len - temp + 1; j++) {
35         a[count - 1][j] = s[t];
36         t++;
37     }
38     a[count - 1][len - temp + 1] = '';
39     
40     for (int i = count - 1; i >= 1; i--) {
41         for (int j = 0; j < len; j++) {
42             if (a[i][j] != '')
43                 cout << a[i][j];
44             else
45                 break;
46         }
47         cout << " ";
48     }
49     for (int j = 0; j < len; j++) {
50         if (a[0][j] != '')
51             cout << a[0][j];
52     }
53     
54     for (int i = 0; i < count; i++) {
55         delete [] a[i];
56     }
57     delete [] a;
58     return 0;
59 }
View Code
原文地址:https://www.cnblogs.com/qinduanyinghua/p/5494608.html