Code from Exercise1: import java.util.Scanner; public class Lab13a { /** * Remov
ID: 3689414 • 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
}
}
Explanation / Answer
Lab13b.java
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<i; 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!");
}
}
Output:
Enter a String :
Howdy
Expanded : owwdddyyyy
Good bye!
Enter a String :
CSE1223
Expanded : SEE111222222222333333
Good bye!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.