I need to make a program that takes a user entered sentence and reverses it with proper formatting and punctuation.
Ex:The quick brown fox jumps over the lazy dog.
RESULT: "Dog lazy the over jumps fox brown quick the."
I have seen solutions to this and I can get a correct answer by asking the user for 1 word at a time. However we are specifically asked to ONLY ask the user for the sentence, and then the program does the rest. So the program has to determine how large the array is, and assign each word in the string to a value in the array (i guess?).
So far this is what I have, however I think that I need to use a stringBuffer but I don't know how to implement this.
public class ReverseSentence {
public static void main(String[] args) {
String[] sentence = new String[]{IO.readString()};
for(int counter = 0; counter < sentence.length; counter++){
System.out.println("Enter Sentence"+(counter+1));
sentence[counter] = IO.readString();
}
System.out.println("The Reversed Sentence is:");
for(int counter = sentence.length - 1; counter >= 0; counter--){
System.out.print(sentence[counter]);
}
}
}
This is not for a homework assignment, just some practice problems for an upcoming exam, so a full solution would be fine, but if possible, with comment lines so I can see how you did it.
IO.readString()return? One word or the whole sentence?