How to reverse a string consisting of a string of word it? Such as: "we go to school" become "school to go we" java code to achieve:
public static String Rever (String str) { // write there is a problem is if there are spaces in the string rearmost split () method is not the last minute out of space. A space in front of the string does not matter the StringBuilder SB = new new the StringBuilder (); // for receiving the MO-table character string String [] = str.split strings ( ""); // on spaces Split // iterate for ( int I = strings.length -. 1; I> = 0; i-- ) { IF (I = 0! ) { sb.append (strings [I] + "" ); } the else { sb.append (strings [I]); } } return sb.toString(); }
If the string-word there are some '', '' how to handle it? Such as: ". We go, to school" becomes ".school to, go we" java achieve the following:
public static String Rever (String STR) { the StringBuilder tempStr = new new the StringBuilder (); // temporarily storing strings the StringBuilder goalStr = new new the StringBuilder (); // finally stored character string for ( int I = str.length () -. 1; I> = 0; I-2019-07-142019-07-142019-07-1410: 27: 59- ) { char C = str.charAt (I); // process the special string if there are other special characters can house if the condition in which if (C == '' || C == ',' || C == ''. ) { goalStr.append (tempStr); // temporarily stored into a target string stored in the character string goalStr.append (C); //The special characters into a target string tempStr.delete (0, tempStr.length ()); // Clear the temporary storage until the next string } the else { tempStr.insert ( 0, C); // non-special character temporary string into the middle to complete the stitching together of words into the target string } } // prevent the front there are no special characters in front of a word will not fit into the IF (! tempStr.equals ( "" )) { goalStr.append ( tempStr); } return goalStr.toString (); }