Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.util.*;
- public class Main {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String encrypted = scanner.nextLine();
- String command = scanner.nextLine();
- while (!command.equals("Finish")) {
- String[] commandParts = command.split("\\s+");
- String commandName = commandParts[0];
- switch (commandName) {
- case "Replace":
- encrypted = replace(encrypted, commandParts[1].charAt(0), commandParts[2].charAt(0));
- break;
- case "Cut":
- int startIndex = Integer.parseInt(commandParts[1]);
- int endIndex = Integer.parseInt(commandParts[2]);
- encrypted = cut(encrypted, startIndex, endIndex);
- }
- command = scanner.nextLine();
- }
- }
- private static String cut(String current, int startIndex, int endIndex) {
- int length = current.length();
- if (!isValidIndex(startIndex, length)) {
- System.out.println("Invalid indexes!");
- return current;
- } else if (!isValidIndex(endIndex, length)) {
- System.out.println("Invalid indexes!");
- return current;
- }
- String firstPart = current.substring(0, startIndex);
- String secondPart = current.substring(endIndex + 1, length);
- String result = firstPart + secondPart;
- System.out.println(result);
- return result;
- }
- private static boolean isValidIndex(int toCheck, int length) {
- return toCheck >= 0 && toCheck < length;
- }
- public static String replace(String current, char searchFor, char replaceWith) {
- String result = current.replace(searchFor, replaceWith);
- System.out.println(result);
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement