杭电oj HDOJ 1062 Text Reverse
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.
题目大意
在一行字符中,若干个“字符块”以“空格”为界,要求:倒序输出每一个“字符块”的内容,并且“字符块”间的顺序不变。
本人的C++解决方案
#include <iostream>
#include <string>
using namespace std;
int main()
{
int T, i, j, k, len;
char ch[1000];
cin>>T;
//Remember to use getchar() to read '\n' after interger T!
getchar();
for (i = 0; i < T; i++) {
gets(ch);
len = strlen(ch);
for (j = 0; j < len; ) {
// 除最后一段“字符块”的倒叙输出
if (ch[j] == ' ') {
k = j;
while (ch[j - 1] != ' ' && j - 1 != -1) {
cout<<ch[j - 1];
j--;
}
cout<<" ";
j = k + 1;
} else {
j++;
}
}
/* 最后一段“字符块”的倒叙输出
如果一行字符中只有一个“字符块”,
则属于“最后一块”的情况!*/
while (ch[j - 1] != ' ' && j != -1) {
cout<<ch[j - 1];
j--;
}
cout<<endl;
}
return 0;
}
代码通过HDOJ平台运行检查,如发现错误,欢迎指出和纠正,谢谢!