Using Java to achieve the reverse order of words _java

Source: Internet
Author: User

In this article, we will arrange the words in the array in reverse order, such as how old are you-> your are old

Sample Program Output results:

The A:
How old are for you!? I Don ' t understand
The second:
Understand don ' t I?! are old

Sample Code

public static void Main (string[] args) {char[] chars= the new String ("How old are you!?"
    I Don ' t understand "). ToCharArray ();
    System.out.println ("The" the "the");
     
    System.out.println (chars); Reversewords (chars);
    Main method System.out.println ("The Second:");
  System.out.println (chars); The/** * will arrange the words in the array in reverse order such as how old are you-> your are old how * @param chars/public static void re
    Versewords (char[] chars) {reversechars (chars,0,chars.length-1);
    int begin =-1;
    int end = 0;
      for (int i=0;i<chars.length;i++) {char c = chars[i]; if ((c>= ' a ' &&c<= ' z ') | | (c>= ' A ' &&c<= ' Z ') | |
          c== ' \ ') {//Simple judgment of whether the word is contiguous if (begin==-1) {begin = i;
        End=i;
          }else{end=i;
          if (i==chars.length-1) {reversechars (chars,begin,end);
          }}else{if (begin!=-1) {reversechars (chars,begin,end);
     Begin=-1;     End=0;
   /** * will arrange char in a range of characters in reverse order such as Hello-> Olleh * @param chars array * @param begin position
      * @param End Position */public static void Reversechars (char[] chars, int begin, int ended) {while (End>begin) {
      char C = chars[begin];
      Chars[begin] = Chars[end];
      Chars[end] = c;
      begin++;
    end--; }
  }

The above is the use of Java to achieve the reverse order of words, I hope to be able to understand that everyone has helped

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.