题解 | #单词倒排#

单词倒排

http://www.nowcoder.com/practice/81544a4989df4109b33c2d65037c5836

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

    Scanner in = new Scanner(System.in);

    String inString = in.nextLine();

    //String inString = "aade *  $afaefeafeafwafaefeafawf* fafaf";

    String result = calc(inString);

    System.out.println(result);

}

public static String calc(String inString){

    StringBuffer sb = new StringBuffer(inString);
    StringBuffer temp = new StringBuffer();

    int count = 0;
    for(int i=0;i<sb.length();i++){
        if(count == 20){
            count = 0;
            temp.append(" ");
        }

        char c = sb.charAt(i);
        if( ( 'A' <= c && c <= 'Z') || ('a' <= c && c <= 'z') ){
            temp.append(c);
            count++;
            continue;
        }

        if(temp.length() == 0){
            continue;
        }

        if(temp.charAt(temp.length()-1) == ' '){
            continue;
        }

        temp.append(" ");
        count = 0;
    }

    String[] res =  temp.toString().split(" ");

    StringBuffer result = new StringBuffer();
    for(int i= res.length-1;i > -1;i--){
        result.append(res[i]).append(" ");
    }

    return result.substring(0,result.length()-1);
}

}

全部评论

相关推荐

10-09 22:05
666 C++
找到工作就狠狠玩CSGO:报联合国演讲,报电子烟设计与制造
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务