Java知识积累2-StringReverse实现文字(单词)倒叙输出

时间:2022-08-04 22:16:44

package String;

import java.util.Stack;
import java.util.StringTokenizer;

public class StringReverse {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String s="hello world!";
method(s);  //适用于英文短句单词倒叙输出
StringBuffer sb=new StringBuffer(s);
System.out.println(sb.reverse());  //适用于汉字倒叙输出

}
public static void method(String s){
Stack myStack=new Stack();
StringTokenizer st=new StringTokenizer(s);
while(st.hasMoreTokens()){
myStack.push(st.nextElement());
}
System.out.println(s+"的逆序输出是:\n\t");
while(!myStack.empty()){
System.out.print(myStack.pop());
//System.out.println(" ");
}
//System.out.println('"');
}

}