Text: JAVA - Data Structures
I am done with Q. 1. But need help with parts 2, 3, AND 4.
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));
This 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.