Offer to prove safety forty-four: Flip word order

Casual working

Cattle recently came off a new employee Fish, every morning and always will be holding a magazine in English, write some sentences in the book. Cat Fish wrote to colleagues interested in the content, look to the day he borrowed Fish, but not read its meaning. For example, "student. A am I". Later I realized that this guy had the sentence word order reversed, the correct sentence should be "I am a student.". Cat on the order of every one of these words do not flip the line, can you help him?

Thinking

Because it is java have a lot of functions for easy operation. Can first be divided by a space, then you can take advantage of the characteristics of StringBuilder.

Thinking

public String ReverseSentence(String str) {
        if(str==null) return null;
      if(str.trim().equals("")) return str;
      String  [] strings=str.split(" ");
   
        StringBuilder strs=new StringBuilder();
        for(int i=strings.length-1;i>=0;i--){
           if(i==0)
               strs.append(strings[i]);
            else
            {
                strs.append(strings[i]);
                strs.append(" ");
            }
        }
        String s=strs.toString();
        return s;
    }
Published 66 original articles · won praise 38 · views 4899

Guess you like

Origin blog.csdn.net/weixin_44015043/article/details/105391760