Todayfreeman

BlogJava 联系 聚合 管理
  4 Posts :: 2 Stories :: 3 Comments :: 0 Trackbacks

面试时曾经碰到过的一个问题。
import java.util.StringTokenizer;

public class Wenzipaixu{

    public static final String SEPARATORS = " ,\t:'';?!";

    public static String reverse(String input){
   
        StringTokenizer st = new StringTokenizer(input, SEPARATORS, true);
        StringBuffer words = new StringBuffer("");
        while (st.hasMoreTokens()) {
            words.insert( 0, st.nextToken() );
        }   
        return words.toString();
    }       
   
    public void testReverse(){
       
        String[] sentences = new String[]{
            "Hello, world!",
            "I am a student",
            "Am I a student? yes, or no",
            "Am I a student ? yes , or no",
            "Zhuang says:'It's just a coding game.'"
            };
           
        for (int i = 0; i < sentences.length; i++)
            System.out.println("Sentence[" + i + "]=[" + sentences[i]+"], " +
                "After reversed: [" + Wenzipaixu.reverse(sentences[i])+"]");
       
    }
   
    public static void main(String[] args){
        new Wenzipaixu().testReverse();
    }
   
       
}

posted on 2006-03-14 19:37 当理想插上程序的翅膀 阅读(195) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: