
Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Lab; Professor: Smith; Class: Introduction to Computing and Programming; Subject: Computer Science; University: University of Georgia; Term: Unknown 2008;
Typology: Lab Reports
1 / 1
This page cannot be seen from the preview
Don't miss anything!

Csci 1301, Lab 7 2008.06. ReverseString.java
Today's lab involves using the length, charAt, and concat String methods to reverse a String that you've read from the user. You’ll also be writing a static method other than main. This is our first taste of method decomposition. I’ve provided a skeleton program. You’ll need to fill in a few bits in the main method and write the body of the reverse method.
In the main method, you'll need to use a Scanner to read the original line of text. Then, you’ll want to call the static method that you’ll be writing: reverse. Remember, it returns a String!
In the reverse method, beginning with an empty result String, use a for loop to read the original String backwards character by character. On each pass through the for loop use the concat method, not the +-operator, to add the appropriate char to the result. You will likely discover a problem: the concat method only takes String arguments! Fortunately, the Character.toString static method takes a char argument and returns that character as a String, so you can use it to turn each char into a length-one String that you can concatenate to the result. Finally, remember that concat does not modify the original String, it creates a new String, so store the result appropriately! Oh, and don’t forget to return the result.
Here's an example of a run of the program:
Enter a String of text: Blah, blah, blah! Reverse String of text: !halb ,halb ,halB