Reverse words in a string (Java note some detail handling)

Source: Internet
Author: User

title:Reverse Words in a string

Given an input string, reverse the string word by word.

For example,
Given s = " the sky is blue ",
Return " blue is sky the ".

parsing: outputting words in a string in reverse order

With the help of a stack, the string was traversed backwards, encountered empty, skipped until non-empty characters, stitched word, and so on again encountered empty, get a word, add a stack,

And so on until the last character of S is traversed.

Finally, the word in the stack is output in turn

Java Encoding:

Method One:

Public String reversewords (string s) {if (s = = NULL | | s.length () = = 0) return s; int index = 0; //The pointer to traverse        int len = S.length (); stack<string> stack = new stack<string> (); Stack, advanced out, sequential save word, reverse output StringBuilder Sbuilder = new StringBuilder ();        Record each word char[] characters = S.tochararray ();            while (Index < len) {//traversal string for (; index < len && Characters[index] = = "; ++index);//Skip NULL characters for (; index < len && Characters[index]! = "; ++index) {//Stitching word sbuilder.append (characters[            Index]);                } if (Sbuilder.length () > 0) {//to press a valid word into the stack Stack.push (sbuilder.tostring ()); Sbuilder.delete (0,sbuilder.length ());//Empty StringBuilder}} sbuilder.delete (0,sbuilder.len Gth ());//Empty StringBuilder while (Stack.size () > 1) {//Not last word, output, plus space Sbuilder.append (stack.pop        () + " ");          if (stack.size () = = 1)//The last word, output, no spaces, here to determine whether the stack is empty, because the possible input string is empty sbuilder.append (Stack.pop ());      return sbuilder.tostring (); }

Method Two:

The regular expression, \s, represents a space, + represents at least one space, so that multiple spaces separated by Word can be extracted.

Public String reversewords (string s) {        string[] words = S.split ("\\s+");   Regular expression        StringBuffer sb = new StringBuffer ();                for (int i = words.length-1; I >= 0; i.) {            sb.append (Words[i] + "");        }                Return sb.tostring (). Trim ();    }

Reverse words in a string (Java note some detail handling)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.