"title" "I am a student." ". Tneduts a ma I". >>student. A AM I
1 PackageCom.exe9.offer;2 3 /**4 * "title" "I am a student." ". Tneduts a ma I". >>student. A AM I5 * @authorWGS6 *7 */8 Public classReversewordsinsentence {9 Public voidReverse (Char[] str,intBeginintend) {Ten if(str==NULL|| str.length<=0)return ; One while(begin<=end) { A Chartemp=Str[begin]; -str[begin]=Str[end]; -str[end]=temp; thebegin++; -end--; - } - + } - + Publicstring getreversesentence (String str) { A if(str==NULL|| Str.length () <=0)returnstr; at Char[] charstr=Str.tochararray (); - //1 Reverse the whole sentence first - intlen=charstr.length; - intBegin=0; - intEnd=len-1; -Reverse (Charstr,begin,end);//. tneduts a ma I inBegin=end=0; - to //2 Invert each word + while(begin<Len) { - //Invalid character If Start is a space the if(charstr[begin]== "){ *begin++; $end++;Panax Notoginseng}Else if(End==len | | charstr[end]== ") {//when end reaches the last one or encounters a space, it indicates the end of a word and begins to reverse the word. -Reverse (Charstr,begin,--end); thebegin=++end; +}Else{ Aend++; the } + } - return NewString (CHARSTR); $ $ } - - Public Static voidMain (string[] args) { theReversewordsinsentence revese=Newreversewordsinsentence (); -String str=revese.getreversesentence ("I am a student."));Wuyi System.out.println (str); the - } Wu -}
The sword refers to the offer series---flip the word order