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

Code from Exercise1: import java.util.Scanner; public class Lab13a { /** * Remov

ID: 3689433 • Letter: C

Question

Code from Exercise1:

import java.util.Scanner;


public class Lab13a {

/**
* Removes all space characters from a String
*
* @param input String to have spaces removed from
* @return the input String with spaces removed
*/
public static String removeSpaces(String input) {
StringBuilder result = new StringBuilder();
for (int i = 0; i < input.length(); i++) {
if (input.charAt(i) != ' ') {
result = result.append(input.charAt(i));
}
}
System.out.println("Without spaces : " +result);
return result.toString();
}

public static void main(String[] args) {
Scanner sc = new Scanner(System.in); // to get the string from the user
System.out.println("Enter a String to de-space : ");
removeSpaces(sc.nextLine()); // calling the method removeSpaces by passing the desired string given by the user

}

}

Code from Exercise 2:

import java.util.Scanner;

public class Lab13b {

   /**
   * expand each character from string by its position.
   *
   * @param input String to have each character expanded from String
   * @return the input String with each character expanded
   */
   public static String expandString(String input) {
   StringBuilder result = new StringBuilder();
   for (int i = 0; i < input.length(); i++) {
   for (int j=0; j    result = result.append(input.charAt(i));
   }
   }
   return result.toString();
   }
   public static void main(String[] args) {
   Scanner sc = new Scanner(System.in); // to get the string from the user
   System.out.println("Enter a String : ");
   String result = expandString(sc.nextLine()); // calling the method expanding each character from string by its position.
   System.out.println("Expanded : " +result);
   System.out.println("Good bye!");
   }
   }

Explanation / Answer

Lab13c.java


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!");
}
}

Output:

Enter a String (empty to quit):
Hello World!
Enter a position to delete (-1 to end) :
5
Current String : HelloWorld!
Enter a position to delete (-1 to end) :
9
Current String : HelloWorl!
Enter a position to delete (-1 to end) :
3
Current String : HeloWorl!
Enter a position to delete (-1 to end) :
7
Current String : HeloWor!
Enter a position to delete (-1 to end) :
-1
Final String : HeloWor!
Enter a String (empty to quit):
Hello World!
Enter a position to delete (-1 to end) :
3
Current String : Helo World!
Enter a position to delete (-1 to end) :
9
Current String : Helo Worl!
Enter a position to delete (-1 to end) :
7
Current String : Helo Wol!
Enter a position to delete (-1 to end) :
3
Current String : Hel Wol!
Enter a position to delete (-1 to end) :
-1
Final String : Hel Wol!
Enter a String (empty to quit):

Good Bye!