Advertisement
valiamaximova1

String java

Apr 3rd, 2021
400
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.02 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class second {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.         String string = scanner.nextLine();
  7.         String input = scanner.nextLine();
  8.  
  9.         while (!input.equals("Finish")) {
  10.             String command = input.split(" ")[0];
  11.             switch (command) {
  12.                 case "Replace":
  13.                     String ToReplace = input.split(" ")[1];
  14.                     String ReplaceWith = input.split(" ")[2];
  15.                     string = string.replaceAll(ToReplace, ReplaceWith);
  16.                     System.out.println(string);
  17.                     break;
  18.                 case "Cut":
  19.  
  20.                     int Start = Integer.parseInt(input.split(" ")[1]);
  21.                     int End = Integer.parseInt(input.split(" ")[2]);
  22.                     if (Start < 0 || End > string.length()) {
  23.                         System.out.println("Invalid indices!");
  24.                         break;
  25.                     }
  26.                     StringBuilder buffer = new StringBuilder(string);
  27.                     buffer.replace(Start, End, "");
  28.                     string = buffer.toString();
  29.                     System.out.println(string);
  30.                     break;
  31.                 case "Make":
  32.                     String Case = input.split(" ")[1];
  33.                     if (Case.equals("Upper")) {
  34.                         string = string.toUpperCase();
  35.                         System.out.println(string);
  36.                         break;
  37.                     }
  38.                     if (Case.equals("Lower")) {
  39.                         string = string.toLowerCase();
  40.                     }
  41.                     System.out.println(string);
  42.                     break;
  43.                 case "Check": {
  44.                     String Check = input.split(" ")[1];
  45.                     if (string.contains(Check)) {
  46.                         System.out.println("Message contains " + Check);
  47.                         break;
  48.                     }
  49.                     System.out.println("Message doesn't contain " + Check);
  50.  
  51.                 }
  52.                 break;
  53.                 case "Sum": {
  54.                     int sum = 0;
  55.                     int start1 = Integer.parseInt(input.split(" ")[1]);
  56.                     int end1 = Integer.parseInt(input.split(" ")[2]);
  57.                     String substring = "";
  58.                     if (start1 < 0 || end1 > string.length()) {
  59.                         System.out.println("Invalid indices!");
  60.                         break;
  61.                     }
  62.                     substring = string.substring(start1, end1 + 1);
  63.                     for (int i = 0; i < substring.length(); i++) {
  64.                         char ch = substring.charAt(i);
  65.                       sum += (int)ch;
  66. //                        int sum += (int)substring[i];
  67.                     }
  68.                     System.out.println(sum);
  69.  
  70.                 }
  71.                 break;
  72.             }
  73.             input = scanner.nextLine();
  74.         }
  75.     }
  76.  
  77. }
  78.  
  79.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement