Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Recursive String Methods In this problem, you will write several static methods

ID: 3633543 • Letter: R

Question

Recursive String Methods
In this problem, you will write several static methods that use recursion to operate on a String.
Requirements:
The methods that you write must be purely recursive. The use of iteration (i.e., for, while, or do-while loops) is not allowed.
The only built-in String methods that you may use are charAt, length, equals, and substring. No use of other String methods is allowed. In addition, make sure to follow any additional restrictions specified in the problem.
Do not use any global variables -- i.e., variables that are declared outside of a method.
Use the headers specified for the methods without changing them in any way.
Limit yourself to writing the methods specified below. Do not write any additional "helper" methods that assist the required methods; rather, the methods listed below should provide all of their required functionality by themselves.
Getting started
Begin by downloading the file StringRecursion.java.
http://cs-people.bu.edu/dgs/courses/cs111/assignments/ps7/StringRecursion.java

The methods
Add to StringRecursion.java your implementations of the following methods, using the headers that are specified:
public static void printEveryOther(String str)

This method should use recursion to print every other character in the string str. For example, a call of printEveryOther("method frame") should produce the following output:
mto rm
(Note that the space between the two words is treated like any other character, and is printed because it is the seventh character in the string.)
The method should not return a value.

Special cases: If the value null or the empty string ("") are passed in as the parameter, the method should just print a newline (by executing the statement System.out.println();).

Hints:

Each call of this method should print only one character from the string; the combination of all of the recursive calls should print the full output string.
When constructing the parameter for the recursive call, you may need to take a slightly different approach than the one that we have typically used when processing strings recursively.
Make sure that your method works correctly for both even-length and odd-length strings.
public static void printTriangle(String str)
This method should use recursion to print a "triangle" that consists of increasingly shorter substrings of the string str. For example, a call of printTriangle("hello") should produce the following output:
hello
hell
hel
he
h
The method should not return a value.
Special cases: If the value null or the empty string ("") are passed in as the parameter, the method should just return without printing anything.

Hint: Here again, when constructing the parameter for the recursive call, you may need to take a slightly different approach than the one that we have typically used when processing strings recursively.

public static boolean contains(String str, char ch)
This method should use recursion to determine if the string specified by the parameter str contains at least one occurrence of the character specified by the parameter ch, returning true if it does and false otherwise. For example:
contains("recursion", 's') should return true
contains("recursion", 'z') should should return false.
This method should not do any printing; it should simply return the appropriate boolean value. In addition, this method may not call the numOccur method that we have given you.

Special cases: If the value null or the empty string ("") are passed in for the first parameter, the method should return false.

public static String replace(String str, char oldChar, char newChar)
This method should return a String that is formed by replacing all occurrences of the character oldChar in the string str with the character newChar. If the character oldChar does not appear in str, the method should just return str. For example:
replace("base case", 'e', 'y') should return "basy casy"
replace("base case", 'r', 'y') should return "base case"
This method should not do any printing; it should simply return the resulting String.

Special cases:
If null is passed in for str, the method should return null.
If the empty string ("") is passed in for str, the method should return the empty string.
Hints:
Like the removeVowels method that we covered in lecture, this method should build up its return value using concatenation, taking the return value of the recursive call and concatenating something to it to create the return value for the current call.
Don't forget that each call of the method should return the appropriate value for its parameters. You may find it helpful to write down the series of method calls that would result from a concrete example, and then write down the appropriate return value for each of those calls. Given the series of return values, think about how you could create the return value of the current call from the return value that it gets back from the recursive call.
public static int lastIndexOf(char ch, String str)
This method should use recursion to find and return the index of the last occurrence of the character ch in the string str, or -1 if ch does not occur in str. For example:
lastIndexOf('r', "recurse") should return 4
lastIndexOf('p', "recurse") should return -1
This method should not do any printing; it should simply return the appropriate integer.

Special cases: If the value null or the empty string ("") are passed in for the second parameter, the method should return -1.

Hints: The second hint for part d also applies to this problem.

Make sure that your methods meet all of the requirements listed at the start of Part II.

Testing your methods
The code that we have given you includes a main method that contains test code for the numOccur and removeVowels methods. One way to test the methods that you will write is to add your own test code to the main method, but you are not required to do so. IMPORTANT: If your method does not return anything -- i.e., it is a void method -- you should NOT try to call it from within a println command. Instead, you should call it on its own line -- for example:
printSeries(3, 8);
Another option is to test the methods from the Interactions Pane. Make sure to try a variety of parameter values, including any values that are treated in a special way.

Explanation / Answer

/* * StringRecursion.java * * starter code by: Computer Science 111, Boston University * * modified by: * username: * * A class that contains recursive methods that operate on strings. */ public class StringRecursion { /* * You may want to add test code for your methods to this main method, * although doing so is not required. See the section of the assignment * entitled "Testing your methods". * * IMPORTANT: If your method does not return anything (i.e., it is a void * method), you should NOT try to call it from within a println command. * Instead, you should call it on its own line -- for example: * printSeries(3, 8); */ public static void main(String[] args) { System.out.println("test 1 gives: " + numOccur('s', "Mississippi")); System.out.println("test 2 gives: " + numOccur('e', "Mississippi")); System.out.println("test 3 gives: " + removeVowels("Mississippi")); System.out.println("test 4 gives: " + removeVowels("apple")); printEveryOther("method frame"); printTriangle("hello"); System.out.println(contains("recursion", 's')); System.out.println(contains("recursion", 'z')); System.out.println(replace("base case",'e','y')); System.out.println(replace("base case",'r','y')); System.out.println(lastIndexOf('r', "recurse")); System.out.println(lastIndexOf('p', "recurse")); } /* * numOccur - a recursive method that returns the number of times that the * character ch occurs in the String str. * * The main method includes two examples of using this method. * * You can also test this method by entering StringRecursion.numOccur(ch, * str) -- where ch is replaced by a char and str is replaced by a string -- * in the Interactions Pane. */ public static int numOccur(char ch, String str) { // base case if (str == null || str.equals("")) { return 0; } // recursive case int numOccurInRest = numOccur(ch, str.substring(1)); if (ch == str.charAt(0)) { return 1 + numOccurInRest; } else { return numOccurInRest; } } /* * removeVowels - a recursive method that creates the String that results * from removing vowels from the String str. * * The main method includes two examples of using this method. * * You can also test this method by entering * StringRecursion.removeVowels(str) -- where str is replaced by a string -- * in the Interactions Pane. * * NOTE: There are examples of debugging printlns included below, but they * have been commented out. */ public static String removeVowels(String str) { // System.out.println("starting removeVowels(" + str + ")"); if (str == null || str.equals("")) { // System.out.println("removeVowels(" + str + ") returns " + str); return str; } String removedFromRest = removeVowels(str.substring(1)); char first = str.charAt(0); // We assume that the string is all lowercase. if (first == 'a' || first == 'e' || first == 'i' || first == 'o' || first == 'u') { // System.out.println("removeVowels(" + str + ") returns " + // removedFromRest); return removedFromRest; } else { // System.out.println("removeVowels(" + str + ") returns " + (first // + removedFromRest)); return first + removedFromRest; } } /* * ****** ADD YOUR METHODS BELOW ********** * * Make sure to use the exact method headers given in the assignment. */ public static void printEveryOther(String str) { if (str == null || str.length() == 0) System.out.println(); else { if (str.length() == 1) System.out.println(str); else { System.out.print(str.charAt(0)); } printEveryOther(str.substring(2)); } } public static void printTriangle(String str) { if (str == null || str.length() == 0) System.out.println(); else { System.out.println(str); printTriangle(str.substring(0, str.length() - 1)); } } public static boolean contains(String str,char ch) { if(str==null || str.length()==0)return false; if(str.charAt(0)==ch)return true; return contains(str.substring(1), ch); } public static String replace(String str,char oldChar,char newChar) { if(str==null || str.length()==0)return str; if(str.charAt(0)==oldChar)return newChar+replace(str.substring(1), oldChar, newChar); return str.charAt(0)+replace(str.substring(1), oldChar, newChar); } public static int lastIndexOf(char ch,String str) { if(str==null || str.length()==0)return -1; if(str.charAt(str.length()-1)==ch)return str.length()-1; return lastIndexOf(ch, str.substring(0,str.length()-1)); } }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote