Advertisement
Zeshin

PROG02

May 13th, 2021
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.01 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class HOMEWORK_PROGRAMMING02 {
  4.     public static void main(String[] args) {
  5.         Scanner scan = new Scanner(System.in);
  6.         String[] input = scan.nextLine().split(" ");
  7.         int n = scan.nextInt();
  8.         scan.nextLine();
  9.         for (int i = 0; i < n; i++) {
  10.             String command = scan.nextLine();
  11.             if (command.toLowerCase().contains("reverse")) {
  12.                 input = ReverseArray(input);
  13.             }
  14.             if (command.toLowerCase().contains("replace")) {
  15.                 input = ReplaceArray(input, command);
  16.             }
  17.             if (command.toLowerCase().contains("distinct")) {
  18.                 input = DistinctArray(input);
  19.             }
  20.         }
  21.         System.out.println(Arrays.toString(input));
  22.     }
  23.  
  24.     private static String[] DistinctArray(String[] input) {
  25.         int counter = 0;
  26.         for(int i =0;i<input.length;i++){
  27.             if(input[i]==null){
  28.                 continue;
  29.             }
  30.             for(int j=i+1;j<input.length;j++){
  31.                 if(input[i].equals(input[j])){
  32.                     input[j] = null;
  33.                     counter++;
  34.                 }
  35.             }
  36.         }
  37.         String[] new_array = new String[input.length - counter];
  38.         int index_counter = 0;
  39.         for (String s : input) {
  40.             if (!(s ==null)) {
  41.                 new_array[index_counter] = s;
  42.                 index_counter++;
  43.             }
  44.         }
  45.         return new_array;
  46.     }
  47.  
  48.     public static String[] ReverseArray(String[] input) {
  49.         for (int j = 0; j < input.length / 2; j++) {
  50.             String temp = input[j];
  51.             input[j] = input[input.length - 1 - j];
  52.             input[input.length - 1 - j] = temp;
  53.         }
  54.         return input;
  55.     }
  56.  
  57.     public static String[] ReplaceArray(String[] input, String command) {
  58.         String[] replaceCommand = command.split(" ");
  59.         input[Integer.parseInt(replaceCommand[1])] = replaceCommand[2];
  60.         return input;
  61.     }
  62. }
  63.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement