[OJ]: Reverse Words in a String

Reverse Words in a String


需求说明

Given an input string, reverse the string word by word.

For example,

Given s = "the sky is blue",

return "blue is sky the".


大致题意

输入一个字符串并翻转这个字符串.

举例,

给定一个字符串"the sky is blue"

返回字符串"blue is sky the"


实现方法

Python

#!/bin/env python
string = raw_input('Please input a string: ')
print " ".join(string.split()[::-1])

Perl

#!/bin/env perl
use strict;
use warnings;
print "Please input a string: ";
my $string = <STDIN>;
print join(' ',reverse(split(' ',$string)))."\n";

Java

import java.util.Scanner;
public class reverseString {
    public static void main(String[] args) {
        Scanner console = new Scanner(System.in);
        System.out.print("Please input a string:");
        String[] string = console.nextLine().split(" ");
        StringBuilder result = new StringBuilder();
        for (int i = string.length - 1; i >= 0; --i) {
            result.append(string[i]).append(" ");
        }
        String reverse =  result.substring(0, result.length()-1);
        System.out.println(reverse);
    }
}

通过上述例子可以看出perl、python处理文本的强大之处.


--------------------------------------------------------------------------------------
版权所有,转载时必须以链接方式注明源地址,否则追究法律责任!

QQ    :   413844436
Email  :   softomg@163.com
Blog   :   http://blog.youkuaiyun.com/softomg

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值