printReversed
Category: Programming
Author: Stuart Reges
Book Chapter: 4.3
Problem: printReversed
Write a static method called printReversed that
takes a String as a parameter and that prints a line of output to
System.out with all words in the String reversed. For this problem, we
will define words as nonempty sequences of characters separated by one or
more spaces. For example, if we make the following calls:
printReversed("four score and seven years ago");
printReversed("our fathers brought forth on this continent");
We should get the following output:
ruof erocs dna neves sraey oga
ruo srehtaf thguorb htrof no siht tnenitnoc
Notice that the words appear in the same order as in the Strings that were
passed as parameters, but each individual word is reversed ("four" has
become "ruof", "score" has become "erocs" and so on).
The String you are passed might have leading or trailing spaces which
should be printed exactly as they appear in the String. For example, if we
make the following call:
printReversed(" merry-go-round is one word ");
the following output should be produced:
dnuor-og-yrrem si eno drow
This output has 3 spaces at the beginning of the line and 2 spaces at the
end of the line, just as in the String.
You are allowed to call the toCharArray method on the String to convert the
entire String into a char[] if you prefer to use array notation rather than
String notation in solving the problem.
Write your solution to printReversed below.