Denis_Hristov

ObrabotkaNaMasiv

Jun 8th, 2021 (edited)
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.07 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class ObrabotkaNaMasiv {
  4.     public static void main(String[] args) {
  5.         Scanner scan = new Scanner(System.in);
  6.  
  7.         System.out.println("Input text: ");
  8.         String text = scan.nextLine();
  9.         String [] splitText = text.split(" ");
  10.  
  11.         System.out.println("Input number of commands: ");
  12.         int n = scan.nextInt();
  13.  
  14.         System.out.println("Input type of command: ");
  15.         for (int i = 0; i <= n; i++) {
  16.             String command = scan.nextLine();
  17.             if (command.equals("reverse")) {
  18.                 Collections.reverse(Arrays.asList(splitText));
  19.             }else if (command.equals("distinct")) {
  20.                 LinkedHashSet<String> distinct = new LinkedHashSet<>(Arrays.asList(splitText));
  21.                 splitText = distinct.toArray(new String[]{});
  22.             }else if (command.contains("Replace")) {
  23.                 splitText[Integer.parseInt(String.valueOf(command.charAt(9)))] = command.substring(11);
  24.             }
  25.         }
  26.         System.out.println(Arrays.toString(splitText));
  27.     }
  28. }
  29.  
Add Comment
Please, Sign In to add comment