Introduction to Computer Science II - Reverse String Java | CSCI 1301, Lab Reports of Computer Science

Material Type: Lab; Professor: Smith; Class: Introduction to Computing and Programming; Subject: Computer Science; University: University of Georgia; Term: Unknown 2008;

Typology: Lab Reports

Pre 2010

Uploaded on 09/17/2009

koofers-user-zam-1
koofers-user-zam-1 🇺🇸

10 documents

1 / 1

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Csci 1301, Lab 7
2008.06.30
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

Partial preview of the text

Download Introduction to Computer Science II - Reverse String Java | CSCI 1301 and more Lab Reports Computer Science in PDF only on Docsity!

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