Advertisement
Guest User

first

a guest
Apr 4th, 2020
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.90 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class first {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.  
  7.         String text = scanner.nextLine();
  8.         String command = scanner.nextLine();
  9.  
  10.         while (!"Done".equals(command)) {
  11.  
  12.             String[] tokens = command.split("\\s");
  13.  
  14.             switch (tokens[0]) {
  15.  
  16.                 case "TakeOdd":
  17.                     StringBuilder sb = new StringBuilder();
  18.  
  19.                     for (int i = 1; i < text.length(); i += 2) {
  20.  
  21.                         sb.append(text.charAt(i));
  22.  
  23.                     }
  24.  
  25.                     text = sb.toString();
  26.  
  27.                     System.out.println(text);
  28.  
  29.  
  30.                     break;
  31.  
  32.                 case "Cut": {
  33.                     int index = Integer.parseInt(tokens[1]);
  34.                     int lenght = Integer.parseInt(tokens[2]);
  35.  
  36.                     String sub = text.substring(index, index + lenght);
  37.  
  38.                    StringBuilder builder = new StringBuilder();
  39.                    builder.append(text);
  40.                    builder.replace(builder.indexOf(sub), builder.indexOf(sub) + sub.length(), "");
  41.  
  42.                    text = builder.toString();
  43.  
  44.                     System.out.println(text);
  45.                 }
  46.                 break;
  47.  
  48.                 case "Substitute":
  49.                     String old = tokens[1];
  50.                     String replecement = tokens[2];
  51.  
  52.                     if (text.contains(old)) {
  53.  
  54.                             text = text.replace(old, replecement);
  55.  
  56.                         System.out.println(text);
  57.                     } else {
  58.                         System.out.println("Nothing to replace!");
  59.                     }
  60.                     break;
  61.             }
  62.  
  63.             command = scanner.nextLine();
  64.         }
  65.  
  66.         System.out.println(String.format("Your password is: %s", text));
  67.  
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement