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

2. RemoveCharacter. Write a method removeCharacter that accepts a string str, a

ID: 3736231 • Letter: 2

Question

2. RemoveCharacter. Write a method removeCharacter that accepts a string str, a character c and an integer n as arguments, such that the string str contains at least n times the character c. The method returns the resulting string after removing the first n occurrences of the character c from the original string str (the method should not print anything to the console). The signature of the method should be: public static String removeCharacter(String str, char c, int n) Use this method to write a program RemoveCharacter.java that takes as command line arguments an integer N followed by N tuples; each tuple consists of a string, a character and an integer (str, c, n). The program should print each of the strings after removing the first n occurrences of the character c from the corresponding string str. Example: » java RemoveCharacter 3 test t 1 shaaaady a 4 testtt t 2 est shdy estt

Explanation / Answer

public class RemoveCharacterDemo {

public static void main(String args[]) {

System.out.println(removeCharacter("Shaaady", 'a', 4));

}

// java function to remove character

public static String removeCharacter(String str, char c, int n) {

String strToReturn="";

int i=0;

while(i < str.length() || n >0) {

if( str.charAt(i) != c)

strToReturn = strToReturn + str.charAt(i);

i++;

n--;

}

return strToReturn.trim();

}

}

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