Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class TextManipulation {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- StringBuilder text = new StringBuilder();
- String command = scanner.nextLine();
- while (!command.equals("End")) {
- String[] tokens = command.split("\\s+"); // split the command by whitespace
- switch (tokens[0]) { // check the first word of the command
- case "Add": // if it is "Add"
- text.append(tokens[1]); // append the given substring at the end of the string
- break;
- case "Upgrade": // if it is "Upgrade"
- char oldChar = tokens[1].charAt(0); // get the given char
- char newChar = (char) (oldChar + 1); // get the next char in the ASCII table
- for (int i = 0; i < text.length(); i++) { // loop through the string
- if (text.charAt(i) == oldChar) { // if the current char matches the given char
- text.setCharAt(i, newChar); // replace it with the next char
- }
- }
- break;
- case "Print": // if it is "Print"
- System.out.println(text); // print the string
- break;
- case "Index": // if it is "Index"
- char target = tokens[1].charAt(0); // get the given char
- boolean found = false; // flag to indicate if any occurences are found
- for (int i = 0; i < text.length(); i++) { // loop through the string
- if (text.charAt(i) == target) { // if the current char matches the given char
- System.out.print(i + " "); // print its index and a space
- found = true; // set the flag to true
- }
- }
- if (!found) { // if no occurences are found
- System.out.print("None"); // print "None"
- }
- System.out.println(); // print a new line
- break;
- case "Remove": // if it is "Remove"
- String substring = tokens[1]; // get the given substring
- int index = text.indexOf(substring); // find the first index of the substring in the string
- while (index != -1) { // while there is a match
- text.delete(index, index + substring.length()); // delete the substring from the string
- index = text.indexOf(substring); // find the next index of the substring in the string
- }
- break;
- }
- command = scanner.nextLine(); // read the next command
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement