Title Description
All the words in the string are inverted.
Description
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 (system.in); 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 em.in); 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