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处理文本的强大之处.
--------------------------------------------------------------------------------------
版权所有,转载时必须以链接方式注明源地址,否则追究法律责任!
Email : softomg@163.com
Blog : http://blog.youkuaiyun.com/softomg