CSCE 145: Lab 16
Fun With Strings

Objectives

The purpose of today's lab is...

Overview

In today's lab we will write a program that can reverse the letters in each word in a simple sentence. For example, "The dog sat." becomes "ehT god tas.".

Program Specification

Create a class, Sentence, that represents a sentence that can be reversed. Also create a driver that can interact with the user and use your Sentence class.

Sentence

This class should have 2 instance variables:

Create accessors and mutators for each variable.

Provide a public method, reverseSentence, that can reverse each word in a simple sentence. You can assume that each word will be separated by exactly one space, there will be a period at the end, and there will be no other punctuation. As a first step to writing this function you should check out the Java API for String: http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html. Use pseudocode and stepwise refinement.

HINT: You can use Scanner not only to read input from System.in (standard input), but also a String.

Driver

Create a driver that will instantiate a Sentence object and interact with the user to get a sentence and display the results of reversing (see sample output below).

Example Output

Welcome to the sentence reverser!

Please enter a simple sentence to reverse:

I saw a cat.

You entered: I saw a cat.

Reversing...

The reversed sentence is:

I was a tac.

Thank you for using my sentence reverser. Goodbye.



Show your program to your TA when you are done and submit it to the dropbox at https://cse.sc.edu.