Java解 | #HJ31单词倒排#倒置字符串# I like beijing # beijing like I #

Posted 闭关苦炼内功

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java解 | #HJ31单词倒排#倒置字符串# I like beijing # beijing like I #相关的知识,希望对你有一定的参考价值。

单词倒排#倒置字符串# I like beijing # beijing like I

jpg0

import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        while(in.hasNext()){
            StringBuffer sb = new StringBuffer(in.nextLine());
            for(int i = 0;i < sb.length(); i++){
                char c = sb.charAt(i);
                // I like beijing    beijing like I
                if(c >= 'A' && c <= 'Z' || c >= 'a' && c <= 'z'){
                // I like beijing.    beijing. like I
                // if(c >= 'A' && c <= 'Z' || c >= 'a' && c <= 'z' || c == '.'){
                    continue;
                }
                sb.replace(i,i+1," ");
            }
            String[] sts = sb.toString().split(" ");
            for(int i=sts.length-1;i>0;i--){
                if(sts[i].equals(" ")){
                    continue;
                }
                System.out.print(sts[i] + " ");
            }
            System.out.println(sts[0]);
        }
    }
}

以上是关于Java解 | #HJ31单词倒排#倒置字符串# I like beijing # beijing like I #的主要内容,如果未能解决你的问题,请参考以下文章

华为机试HJ31:单词倒排

Java解 | #HJ1字符串最后一个单词的长度#

Java解 | #HJ14字符串排序#

Java解 | #HJ4字符串分隔#

Java解 | #HJ10字符个数(种类)统计#

Java解 | #HJ59找出字符串中第一个只出现一次的字符#