Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Nikulden {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String string = scanner.nextLine();
- String[] commands = scanner.nextLine().split(" ");
- while (!commands[0].equals("Finish")) {
- if (commands[0].equals("Replace")) {
- string = string.replace(commands[1].charAt(0), commands[2].charAt(0));
- System.out.println(string);
- }
- if (commands[0].equals("Cut")) {
- int startIndex = Integer.parseInt(commands[1]);
- int endIndex = Integer.parseInt(commands[2]);
- if (startIndex < 0 || endIndex > string.length()-1 || endIndex < 0 || startIndex > string.length()-1||startIndex>=endIndex) { //check
- System.out.println("Invalid indexes!");
- } else {
- String stringToBeCut = string.substring(Integer.parseInt(commands[1]), Integer.parseInt(commands[2])+1);
- string = string.replace(stringToBeCut, "");
- System.out.println(string);
- }
- }
- if (commands[0].equals("Make")) {
- if (commands[1].equals("Upper")) {
- string = string.toUpperCase();
- System.out.println(string);
- }
- if (commands[1].equals("Lower")) {
- string = string.toLowerCase();
- System.out.println(string);
- }
- }
- if (commands[0].equals("Check")) {
- if (string.contains(commands[1])) {
- System.out.printf("Message contains %s%n", commands[1]);
- } else {
- System.out.printf("Message doesn't contain %s%n", commands[1]);
- }
- }
- if (commands[0].equals("Sum")) {
- int startIndex = Integer.parseInt(commands[1]);
- int endIndex = Integer.parseInt(commands[2]) + 1;
- if (startIndex < 0 || endIndex >= string.length() || endIndex < 0 || startIndex >= string.length()) { //check
- System.out.println("Invalid indexes!");
- } else {
- String substring = string.substring(startIndex, endIndex);
- int sum = 0;
- for (int i = 0; i < substring.length(); i++) {
- char nextChar = substring.charAt(i);
- sum += nextChar;
- }
- System.out.println(sum);
- }
- }
- commands = scanner.nextLine().split(" ");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement