您的位置:首页 > 其它

hdu-1062 Text Reverse

2016-07-12 20:41 453 查看


Text Reverse

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 25953    Accepted Submission(s): 10068


Problem Description

Ignatius likes to write words in reverse way. Given a single line of text which is written by Ignatius, you should reverse all the words and then output them.

 

Input

The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.

Each test case contains a single line with several words. There will be at most 1000 characters in a line.

 

Output

For each test case, you should output the text which is processed.

 

Sample Input

3
olleh !dlrow
m'I morf .udh
I ekil .mca

 

Sample Output

hello world!
I'm from hdu.
I like acm.

Hint
Remember to use getchar() to read '\n' after the interger T, then you may use gets() to read a line and process it.
题目看清楚,问题其实还不大,不是将整个字符串反转过来,而是一个字符串中包含了一个或者多个单词,将其中的各个单词反转过来顺序就对了,代码如下:
//
//  main.cpp
//  hdu1062
//
//  Created by Morris on 16/7/12.
//  Copyright © 2016年 Morris. All rights reserved.
//

#include <cstdio>
#include <cstring>

namespace {
using std::scanf;
using std::printf;
using std::getchar;
using std::gets;
using std::strlen;
}

int main(int argc, const char *argv[])
{
int n;
char word[100];
char str[2048];
int i, j, k, l, m;
while (~scanf("%d", &n)) {
getchar();
for (i = 0; i < n; ++i) {
gets(str);
j = k = l = 0;
while (k < strlen(str)) {
if (str[k] != ' ') {
word[l++] = str[k];
}
else {
for (m = l - 1; m >= 0; --m) {
printf("%c", word[m]);
}
printf(" ");

l = 0;
}

if (k == strlen(str) - 1) {
for (m = l - 1; m >= 0; --m) {
printf("%c", word[m]);
}
printf("\n");
}

++k;
}
}
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ACM