Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- import java.util.Collections;
- import java.util.List;
- import java.util.Scanner;
- import java.util.stream.Collectors;
- public class _03_TheFinalQuest {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- List<String> words = Arrays.stream(scanner.nextLine()
- .split(" "))
- .collect(Collectors.toList());
- String input = "";
- while (!"Stop".equals(input = scanner.nextLine())) {
- String[] line = input.split(" ");
- String command = line[0];
- switch (command) {
- case "Delete":
- int index = Integer.parseInt(line[1]);
- if (index < words.size() - 1) {
- words.remove(index + 1);
- }
- break;
- case "Swap":
- String word1 = line[1];
- String word2 = line[2];
- if (words.contains(word1) && words.contains(word2)) {
- int firstInd = words.indexOf(word1);
- int secondInd = words.indexOf(word2);
- words.set(firstInd, word2);
- words.set(secondInd, word1);
- }
- break;
- case "Put":
- String word = line[1];
- int putInd = Integer.parseInt(line[2]);
- if ((putInd - 1) >= 0 && (putInd - 1) <= words.size()) {
- words.add(putInd - 1, word);
- }
- break;
- case "Sort":
- Collections.sort(words);
- Collections.reverse(words);
- break;
- case "Replace":
- String firstW = line[1];
- String secW = line[2];
- if (words.contains(secW)) {
- int ind = words.indexOf(secW);
- words.set(ind, firstW);
- }
- break;
- }
- }
- String output = words.toString().replaceAll("[\\[,\\]]", "");
- System.out.println(output.trim());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement