There are some assignments in Walter Savitch's Java book, where it asks you to write some code to reverse the order of a word that is entered. I came up with the following and am wondering if I could be able to optimize it as it seems a little heavy:
public static void main(String[] args) {
String statement;
System.out.print("Enter a statement to reverse: ");
statement = keyboard.nextLine();
int n;
String finalWord = "";
String letter;
for (n = statement.length(); n > 0; n--)
{
letter = statement.substring(0, 1);
finalWord = letter + finalWord;
statement = statement.substring(1);
System.out.println(finalWord);
}
System.out.println("Final work: " + finalWord);
Any insight would be appreciated. }