面试时曾经碰到过的一个问题。
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();
}
}