杭电oj HDOJ 1062 Text Reverse

杭电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平台运行检查,如发现错误,欢迎指出和纠正,谢谢!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值