Scrivere un metodo che trasponga le parole di una frase. Si supponga che la frase di
ingresso non contenga punteggiatura, che ci sia solo uno spazio tra due parole
e che la frase di ingresso inizi con un carattere diverso dalla spazio. Esempio:
Input: The gate to Java nirvana is near
Output: ehT etag ot avaJ anavrin si raen
public void transposeWords(String str) {
StringBuffer string = new StringBuffer(str);
String substr = new String();
int beginIndex = 0;
int endIndex = -1;
do {
beginIndex = endIndex + 1;
endIndex = string.indexOf(" ", beginIndex);
if( endIndex == -1 )
endIndex = string.length();
substr = string.substring(beginIndex, endIndex);
substr = (new StringBuffer(substr).reverse()).toString();
string.replace(beginIndex, endIndex, substr);
} while (endIndex != string.length());
System.out.println(string);
}