A program SentenceReverser that reverses the words in a sentence by reading words into a Stack until
Question:
A program SentenceReverser that reverses the words in a sentence by reading words into a Stack until you find a period. Your program should then pop off the words from the stack. Begin your reverse sentence with a capital letter and end it with a period. Your program should be able to handle multiple sentences. Use while (scan.hasNext()) to capture your input. Please no spaces before the period. Also, the first letter of your sentences should start with upper case.
Mary had a little lamb. His fleece was as white as snow.
Becomes:
Lamb little a had mary. Snow as white as was fleece his.
Note: Your test input should include multiple sentences as shown above. There should NOT be a space before your periods.
Hints:
1) The Scanner class can be used to break a String into a words using the space character as the delimeter.
public String reverse(String sentence) { String reversed = ""; Stack stack = new Stack<>(); Scanner scanner = new Scanner(sentence) // Get the words in a sentence while (scanner.hasNext()) { String pushWord = scanner.next(); // your code here. // Call a method processSentence when you find a period. } return reversed; public String processSentence(Scanner scanner, Stack stack, String reversed) { // your code here. }
2. How to remove space before a period.
popWord = popWord.replace(".", "");
3. Mask first letter of a word upper case:
String firstLetter = word.substring(0, 1); word = firstLetter.toUpperCase() + word.substring(1);
4. Make word all lower case:
word= word.toLowerCase();
International Marketing And Export Management
ISBN: 9781292016924
8th Edition
Authors: Gerald Albaum , Alexander Josiassen , Edwin Duerr