Question: Java - Data Structures Q. Write a recursive method to reverse a string. Use the StringBuilder to hold the string. Here are some StringBuilder ideas.

Java - Data Structures

Q.

Write a recursive method to reverse a string.

Use the StringBuilder to hold the string. Here are some StringBuilder ideas.

1. There are two ways to put a string into a StringBuilder. You can do it in one line:

StringBuilder myString = new StringBuilder ("ABCDEFG);

or with two lines. The first line creates the StringBuilder object:

StringBuilder myString;

and the second line assigns it a value:

myString = new StringBuilder ("ABCDEFG");

Of course, the only reason to use two lines is if you want to create the variable first and store something in it later.

2. To access a particular character in a StringBuilder object use the charAt method:

System.out.println (myString.charAt(2));

will print C. Remember, the first character in the StringBuilder object is character 0.

3. To work with the last character, use:

myString.charAt(myString.length()-1)

4. To delete character 3, use

myString.deleteCharAt (3);

This deletes the character D, since the first character is character 0.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!