Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class second {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String string = scanner.nextLine();
- String input = scanner.nextLine();
- while (!input.equals("Finish")) {
- String command = input.split(" ")[0];
- switch (command) {
- case "Replace":
- String ToReplace = input.split(" ")[1];
- String ReplaceWith = input.split(" ")[2];
- string = string.replaceAll(ToReplace, ReplaceWith);
- System.out.println(string);
- break;
- case "Cut":
- int Start = Integer.parseInt(input.split(" ")[1]);
- int End = Integer.parseInt(input.split(" ")[2]);
- if (Start < 0 || End > string.length()) {
- System.out.println("Invalid indices!");
- break;
- }
- StringBuilder buffer = new StringBuilder(string);
- buffer.replace(Start, End, "");
- string = buffer.toString();
- System.out.println(string);
- break;
- case "Make":
- String Case = input.split(" ")[1];
- if (Case.equals("Upper")) {
- string = string.toUpperCase();
- System.out.println(string);
- break;
- }
- if (Case.equals("Lower")) {
- string = string.toLowerCase();
- }
- System.out.println(string);
- break;
- case "Check": {
- String Check = input.split(" ")[1];
- if (string.contains(Check)) {
- System.out.println("Message contains " + Check);
- break;
- }
- System.out.println("Message doesn't contain " + Check);
- }
- break;
- case "Sum": {
- int sum = 0;
- int start1 = Integer.parseInt(input.split(" ")[1]);
- int end1 = Integer.parseInt(input.split(" ")[2]);
- String substring = "";
- if (start1 < 0 || end1 > string.length()) {
- System.out.println("Invalid indices!");
- break;
- }
- substring = string.substring(start1, end1 + 1);
- for (int i = 0; i < substring.length(); i++) {
- char ch = substring.charAt(i);
- sum += (int)ch;
- // int sum += (int)substring[i];
- }
- System.out.println(sum);
- }
- break;
- }
- input = scanner.nextLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement