日期:2014-05-20  浏览次数:20611 次

两道java编程题,求解
18.String s = "113@ ere qqq yyui"
请输出所有子串
113
ere
qqq
yyui


19. 编写一个程序,将下面的一段文本中的各个单词的字母顺序翻转,
“To be or not to be",将变成"oT eb ro ton ot eb."。



------解决方案--------------------
汗,贴错地方了

Java code

    public static void main(String[] args) throws Exception {

        String s = "113@ ere qqq yyui";
        s=s.replaceAll("[^a-zA-Z 0-9]", "");
        for (String str: s.split(" ")) {
            System.out.println(str);
        }
        
        
        //末尾标点是不是不要反转?
        s = "To be or not to be.";//"oT eb ro ton ot eb."。
        StringBuffer sBuffer = new StringBuffer();
        for (String str: s.split("\\s")) {
            char punctuate=' ';
            char[] chars = str.toCharArray();
            for (int i = chars.length-1;i>=0;i--) {
                if (i==chars.length-1 && !Character.isLetter(chars[i])) {
                    punctuate = chars[i];
                    continue;
                }
                sBuffer.append(chars[i]);
            }
            sBuffer.append(punctuate);
        }
        System.out.println(sBuffer);
    }