RGAlex

Test5

Apr 3rd, 2021
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.45 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.stream.IntStream;
  3.  
  4. public class test {
  5.     public static void main(String[] args) {
  6.         Scanner scanner = new Scanner(System.in);
  7.         String text = scanner.nextLine();
  8.         String input;
  9.  
  10.         while (!"Finish".equals(input = scanner.nextLine())) {
  11.             String[] command = input.split("\\s+");
  12.             switch (command[0]) {
  13.                 case "Replace":
  14.                     text = text.replaceAll(command[1], command[2]);
  15.                     System.out.println(text);
  16.                     break;
  17.                 case "Cut":
  18.                     if (isValid(Integer.parseInt(command[1]), Integer.parseInt(command[2]), text.length())) {
  19.                         text = new StringBuilder(text).delete(Integer.parseInt(command[1]),
  20.                                 Integer.parseInt(command[2]) + 1).toString();
  21.                         System.out.println(text);
  22.                     } else {
  23.                         System.out.println("Invalid indices!");
  24.                     }
  25.                     break;
  26.                 case "Make":
  27.                     if ("Upper".equals(command[1])) {
  28.                         text = text.toUpperCase();
  29.                     } else {
  30.                         text = text.toLowerCase();
  31.                     }
  32.                     System.out.println(text);
  33.                     break;
  34.                 case "Check":
  35.                     if (text.contains(command[1])) {
  36.                         System.out.printf("Message contains %s%n", command[1]);
  37.                     } else {
  38.                         System.out.printf("Message doesn't contains %s%n", command[1]);
  39.                     }
  40.                     break;
  41.                 case "Sum":
  42.                     if (isValid(Integer.parseInt(command[1]), Integer.parseInt(command[2]), text.length())) {
  43.                         String sub = text.substring(Integer.parseInt(command[1]), Integer.parseInt(command[2]) + 1);
  44.                         int sum = IntStream.range(0, sub.length()).map(sub::charAt).sum();
  45.                         System.out.println(sum);
  46.                     } else {
  47.                         System.out.println("Invalid indices!");
  48.                     }
  49.                     break;
  50.             }
  51.         }
  52.     }
  53.     private static boolean isValid(int startIndex, int endIndex, int length) {
  54.         return (startIndex >= 0 && startIndex < length) && (endIndex >= 0 && endIndex < length);
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment