Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

OpenStack Trove

Authors: Amrith Kumar, Douglas Shelley

1st Edition

1484212215, 9781484212219

More Books

Students also viewed these Databases questions