Huawei oj--Word Inverted line

Source: Internet
Author: User

Title Description

All the words in the string are inverted.


1. Each word is composed of 26 uppercase or lowercase English letters;

2. Characters of non-constituent words are regarded as word spacers;

3. The word spacer is required to be inverted after a blank space; if there are multiple spacers between adjacent words in the original string, only one space spacer is allowed after the inverted conversion;

4, the maximum of 20 letters per word;

Input Description:

Enter a line of sentences separated by a space

Output Description:

Output the inverse of a sentence

Input Example:
I am a Student
Output Example:
Student a AM I
method One:

Import java.util.*;p ublic class main{public static void Main (string[] args) {Scanner scan=new Scanner (; while ( Scan.hasnext ()) {String input=scan.nextline (); char[] Chars=input.tochararray (); for (int i=0;i<input.length (); i++ {char ch=chars[i];if (!) ( Ch>= ' A ' && ch<= ' z ') &&! (ch>= ' A ' && ch<= ' Z ')) Chars[i]= ';} String str=string.valueof (chars); String[] Strs=str.split (""); for (int i=strs.length-1;i>=0;i--) {if (!strs[i].equals ("")) {System.out.print (strs[i ]+" ");} else {System.out.print (strs[i]);}}}}

Method Two (regular expression):

Import Java.util.scanner;public class main{public    static void Main (string[] args) {        Scanner sc = new Scanner (Syst;        String str = "";        while (Sc.hasnext ())        {            str = sc.nextline ();            string[] STRs = Str.split ("[^a-za-z]+");            for (int i=strs.length-1;i>=0;i--)            {                if (i = = 0)                {                    System.out.println (strs[i]);                }                else                {                    System.out.print (strs[i]+ "");                }        }}     }

Huawei oj--Word Inverted line

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: 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.