Enter a string (length < = 40 characters) from the keyboard and end with the character '.'. The editing function includes: 1 D: delete a character. The command method is: D a where a is the deleted character, for example: D s means delete character's' s'. If there are multiple 's' in the string, delete the first occurrence.
2 I: insert a character. The format of the command is: I a1 a2 where a1 represents the character to be inserted before the specified character, and a2 represents the character to be inserted. For example: I s d means to insert the character 'd' before the specified character 's'. If there are more than one' s' in the original string, it will be inserted before the last character.
3 R: replace a character. The command format is R a1 a2, where a1 is the replaced character and a2 is the replaced character. If there are multiple a1 in the original string, replace them all.
In the editing process, if the modified character does not exist, a prompt message will be given.
Input format
There are two lines in the input file stringedit.in, the first line is the original string (ending with '.'), and the second line is the command (see "problem description" for the input method).
Output format
The output file, stringedit.out, has one line in total. It is used to specify the prompt information that the character does not exist for the modified string or output.
sample input
This is a book.
D s
sample output
Thi is a book.
D s
Input and output sample explanation
The command is to delete s. The first s that appears in the character is in This, which is the result.
File read and write operations are added based on the original
Three judgments make full use of the advantages of string
If you use C to write, you need to write the linked list yourself. C + + or JAVA provides the operation to convert the list, but you can use the api to solve this problem.
package Judge; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class Main { static String sequence; static String command; static String result="Output does not exist"; public static void main(String[] args) { fileRead(); if(command.contains("D")) { //D operation judgment char temp = command.charAt(2); if(sequence.contains(String.valueOf(temp))) { //Determine whether the character exists in the string to be processed //Remove the first occurrence of this character result = sequence.replaceFirst(String.valueOf(temp), ""); } }else if(command.contains("I")) { //I judgment of operation char temp = command.charAt(2); char temp2 = command.charAt(4); if(sequence.contains(String.valueOf(temp))) { //Add a character before the character that appears //Cut out the prefix and suffix, and then splice the characters between the prefix and suffix. String pre = sequence.substring(0, sequence.lastIndexOf(String.valueOf(temp))); String suf = sequence.substring(sequence.lastIndexOf(String.valueOf(temp)) ,sequence.length()); result = pre+temp2+suf; } }else if(command.contains("R")) { //Determination of R operation char temp = command.charAt(2); char temp2 = command.charAt(4); if(sequence.contains(String.valueOf(temp))) { //All occurrences are replaced with the second character result = sequence.replaceAll(String.valueOf(temp), String.valueOf(temp2)); } } System.out.println(result); fileWrite(); } public static void fileRead() { FileReader fileReader = null; BufferedReader br = null; try { fileReader = new FileReader("src\\stringedit.in"); br = new BufferedReader (fileReader); sequence = br.readLine(); command = br.readLine(); } catch (IOException e) { e.printStackTrace(); }finally { try { br.close(); fileReader.close(); } catch (IOException e) { e.printStackTrace(); } } } public static void fileWrite() { FileWriter fileWriter = null; BufferedWriter bw = null; try { fileWriter = new FileWriter("src\\stringedit.out"); bw = new BufferedWriter (fileWriter); bw.write(result); } catch (IOException e) { e.printStackTrace(); }finally { try { bw.close(); fileWriter.close(); } catch (IOException e) { e.printStackTrace(); } } } }