Advertisement
Guest User

Untitled

a guest
Dec 10th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.78 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class ne {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.  
  7.         String string = scanner.nextLine();
  8.         String[] commands = scanner.nextLine().split(" ");
  9.  
  10.         while (!commands[0].equals("Finish")) {
  11.  
  12.             if (commands[0].equals("Replace")) {
  13.  
  14.                 string = string.replace(commands[1].charAt(0), commands[2].charAt(0));
  15.                 System.out.println(string);
  16.  
  17.             }
  18.  
  19.             if (commands[0].equals("Cut")) {
  20.                 int startIndex = Integer.parseInt(commands[1]);
  21.                 int endIndex = Integer.parseInt(commands[2]);
  22.                 if (startIndex < 0 || endIndex > string.length()/*-1*/ || endIndex < 0 || startIndex > string.length()/*-1*/||startIndex >/*=*/endIndex)  {   //check
  23.  
  24.                     System.out.println("Invalid indexes!");
  25.  
  26.                 } else {
  27.  
  28.                     String stringToBeCut = string.substring(Integer.parseInt(commands[1]), Integer.parseInt(commands[2])+1);
  29.                     string = string.replace(stringToBeCut, "");
  30.                     System.out.println(string);
  31.                 }
  32.  
  33.             }
  34.  
  35.             if (commands[0].equals("Make")) {
  36.                 if (commands[1].equals("Upper")) {
  37.                     string = string.toUpperCase();
  38.                     System.out.println(string);
  39.                 }
  40.  
  41.                 if (commands[1].equals("Lower")) {
  42.                     string = string.toLowerCase();
  43.                     System.out.println(string);
  44.                 }
  45.  
  46.             }
  47.  
  48.             if (commands[0].equals("Check")) {
  49.  
  50.  
  51.                 if (string.contains(commands[1])) {
  52.                     System.out.printf("Message contains %s%n", commands[1]);
  53.                 } else {
  54.  
  55.                     System.out.printf("Message doesn't contain %s%n", commands[1]);
  56.                 }
  57.  
  58.             }
  59.  
  60.             if (commands[0].equals("Sum")) {
  61.  
  62.                 int startIndex = Integer.parseInt(commands[1]);
  63.                 int endIndex = Integer.parseInt(commands[2]) + 1;
  64.                 if (startIndex < 0 || endIndex >/*=*/ string.length() || endIndex < 0 || startIndex >/*=*/ string.length()) {   //check
  65.  
  66.                     System.out.println("Invalid indexes!");
  67.                 } else {
  68.  
  69.                     String substring = string.substring(startIndex, endIndex);
  70.                     int sum = 0;
  71.  
  72.                     for (int i = 0; i < substring.length(); i++) {
  73.  
  74.  
  75.                         char nextChar = substring.charAt(i);
  76.                         sum += nextChar;
  77.  
  78.                     }
  79.                     System.out.println(sum);
  80.                 }
  81.             }
  82.  
  83.             commands = scanner.nextLine().split(" ");
  84.         }
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement