
Question
反转给定的句子,而不反转单个单词。 We provide good material for IT Technical Interview preparation
输出: preparation Interview Technical IT for material good provide We
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader;
public class A050 {
public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); //System.out.println("Enter the string"); String s = br.readLine(); String rev = StringRev(s); System.out.println(rev); }
private static String StringRev(String s) { char[] modString = new char[s.length()]; for (int i = 0; i < s.length(); i++) { modString[i] = s.charAt(s.length() - 1 - i); } s = s.copyValueOf(modString); String reverseWord = ""; String eachWord; for (String part : s.split(" ")) { eachWord = new StringBuilder(part).reverse().toString(); reverseWord = reverseWord + eachWord + " "; } return reverseWord; } }
|
近期评论