Write a program (from scratch, if you can -- good practice for a test) named ResreveR. This program asks the User to type in a sentence (words separated by spaces and ending with a period). The sentence cannot be longer than 20 words.

The program stores the words of the sentence in an array of Strings (a String[ ] ) in correct order (first word is in array element 0). Then the program prints out the words of the sentence backwards. That is, the last word of the sentence is followed by the next-to-last, etc. The letters in the words do not need to be reversed; just reverse the order of the words.

help

04-26-2009, 02:35 AM

Fubarable

Hey Einstein, and welcome back. A couple of pointers:

1) break this problem down into smaller steps
* prompt user
* get input and place into String
* break String into words
* put words into a String array, backwards order
* display results

So start from the top and work on each individual problem, one at a time.

2) help is a poor question which I'm sure that you know. Remember, the more specific your question is, the more specific your answers are likely to be.

3) If you get stuck on a particular step, then come back with your code attempt and a specific question.

// TODO: debug code. remove from final program
for (int i = 0; i < words.length; i++)
{
System.out.println(words[i]);
}
// TODO: debug code. remove from final program
}
}

Also, please don't forget to use code tags when posting code.

edit: if you run this, you'll see a bunch of "null" String results in the output. This is important to know: if the input String is < 20 words (and most will likely be less than 20), the first part of the array will be filled with words, but the rest of the array will be filled with null values. This is important when you try to reverse your String. You could check to make sure that the array item is not null with a statement like so:

Code:

if word[i] != null
{
// this item is OK to use.
}

Also, when you get down to the business of reversing your String array, you'll likely have two arrays, your word String array, and another one, perhaps called reverseWord, or whatever you'd like. You'll likely use a for loop to do the placement of Strings from one array to the other, but you will need to be very careful with what indexes you need to use, that is the numbers that go in the square brackets: word[index number goes here]. You should work out on paper how to handle this. It's not hard to do and only requires some simple math.