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

Code from Exercise 3: package osu.cse1223; import java.io.BufferedReader; import

ID: 3689477 • Letter: C

Question

Code from Exercise 3:

package osu.cse1223;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Lab13c {
   /**
   * delete a character from String based on char position.
   *
   * @param input String to delete specific char based on its position
   * @return the input String after delete a char.
   */
   public static String removeCharacterFromString(String input, int n) {
   StringBuilder result = new StringBuilder();
   for (int i = 0; i < input.length(); i++) {
   if (i != n) {
   result = result.append(input.charAt(i));
   }
   }
   return result.toString();
   }
   public static void main(String[] args) throws NumberFormatException, IOException {
   BufferedReader buf=new BufferedReader(new InputStreamReader(System.in));// to get the string from the user
  
   int n = 0;
   while(true){
   System.out.println("Enter a String (empty to quit): ");
   String s = buf.readLine();
   if(!s.isEmpty()){

   do{
   System.out.println("Enter a position to delete (-1 to end) : ");
   n = Integer.parseInt(buf.readLine());
   if(n >= 0){
   s = removeCharacterFromString(s, n);
   System.out.println("Current String : "+s);
   }
   else{
   break;
   }

   }while(!s.isEmpty());
   n=0;
   System.out.println("Final String : "+s);
   }
   else{
       break;
   }
   }
   System.out.println("Good Bye!");
   }
}

Exercise 4 If you have time, create a copy of your solution for Exercise 3 and name it Lab13d.java in the same ClosedLab13 folder. For this exercise, modify your program to remove substrings of the original string instead of positions. As a hint towards how to go about doing this, take a look at the StringBuilder methods indexOf and delete. As in Exercise 3, you can write methods to do this or do it all in the main method for this closed lab question. A sample transcript appears below: Enter a string (empty to quit): Hello World! Enter a substring to delete (empty to endorld Current string: Hello W! Enter a position to delete (empty to end) lo Current string: Hel W! Enter a position to delete (empty to end): He Current string: 1 W! Enter a position to delete (empty to end)! Current string: 1 W Enter a position to delete (empty to end): Final String: l W Enter a string (empty to quit):

Explanation / Answer

Lab13d.java


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Lab13d {
/**
* delete a character from String based on char position.
*
* @param input String to delete specific char based on its position
* @return the input String after delete a char.
*/
public static String removeSubStrFromString(String input, String subStr) {
StringBuilder result = new StringBuilder(input);
int i = result.indexOf(subStr);
if (i != -1) {
   result = result.delete(i, i + subStr.length());
}
return result.toString();
}
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader buf=new BufferedReader(new InputStreamReader(System.in));// to get the string from the user
  
int n = 0;
while(true){
System.out.println("Enter a String (empty to quit): ");
String s = buf.readLine();
if(!s.isEmpty()){
do{
System.out.println("Enter a substring to delete (empty to quit) : ");
String subStr = buf.readLine();
if(!subStr.isEmpty()){
s = removeSubStrFromString(s, subStr);
System.out.println("Current String : "+s);
}
else{
break;
}
}while(!s.isEmpty());
n=0;
System.out.println("Final String : "+s);
}
else{
break;
}
}
System.out.println("Good Bye!");
}
}

Output:

Enter a String (empty to quit):
Hello World!
Enter a substring to delete (empty to quit) :
orld
Current String : Hello W!
Enter a substring to delete (empty to quit) :
lo
Current String : Hel W!
Enter a substring to delete (empty to quit) :
he
Current String : Hel W!
Enter a substring to delete (empty to quit) :
He
Current String : l W!
Enter a substring to delete (empty to quit) :
!
Current String : l W
Enter a substring to delete (empty to quit) :

Final String : l W
Enter a String (empty to quit):

Good Bye!

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