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

On some old Unix terminals, pressing the Backspace (delete) key generates the ch

ID: 3585232 • Letter: O

Question

On some old Unix terminals, pressing the Backspace (delete) key generates the character sequence AH (Control-H) to delete the previously-typed character. Some people use this for sarcastic effect when writing, to pretend that they are deleting or replacing some insulting comment Be nice to this foolHAHAHHgentleman, he's visiting from corporate HQ. Define a Java method named backspace() that takes a single String argument. This method returns a new String where each "AH" sequence from the argument has been removed and applied to the non-Backspace character immediately preceding the "AH". For example, given the input above, the program would produce the output: Be nice to this gentleman, he's visiting from corporate HQ. where each "AH" is missing and has effectively erased a preceding character from "fool". Your program should only perform erasures for a full "H" character digram; other

Explanation / Answer

package shivCode;

import java.util.Scanner;

public class Replace {

public static String backspace(String input) {

while(input.contains("^H")) {

int index = input.indexOf("^H");

int len = input.length();

if (index == 1) {

if (len == 3) {

return "";

}

input = input.substring(3);

} else {

String result = input.substring(0, index-1);

if (index != len -2) {

result += input.substring(index+2);

}

input = result;

}

}

return input;

}

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter a string to replace: ");

System.out.println(backspace(sc.nextLine()));

}

}

// Sample run

Enter a string to replace:
1234^H
123

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