翻转“I am a student.”得到“students. a am I”。单词以空格间隔为标准.
public class Test2 {
public void reverse(){
String inputStr = "";
InputStreamReader inputStream = new InputStreamReader(System.in);
BufferedReader reader = new BufferedReader(inputStream);
try
{
System.out.println("Please input a string in the below command line:");
inputStr=reader.readLine();
}
catch(IOException e)
{
System.out.println("I/O error occurs!");
System.out.println(e.getMessage());
e.printStackTrace();
}
StringBuilder sb=new StringBuilder(inputStr);
StringBuilder sb2=new StringBuilder();
String [] sbCopy=sb.toString().split(" ");
for(int i=sbCopy.length-1;i>=0;i--){
StringBuilder sb3=new StringBuilder(sbCopy[i]);
sb2.append(sb3+" ");
}
System.out.println(sb2);
}
public static void main(String []args){
(new Test2()).reverse();
}
}