Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class SecretChat {
- public static void main(String[] args) {
- // 1 greshka 87 / 100
- Scanner scan = new Scanner(System.in);
- String chatWord = scan.nextLine();
- String command = scan.nextLine();
- while (!command.equals("Reveal")) {
- String[] commands = command.split("\\:\\|:");
- switch (commands[0]) {
- case "ChangeAll":
- String searchFor = commands[1];
- String replaceWith = commands[2];
- chatWord = chatWord.replaceAll(searchFor, replaceWith);
- System.out.println(chatWord);
- break;
- case "Reverse":
- String word = commands[1];
- if (chatWord.contains(word)){
- String reverse = reverse(word); //1-vi metod
- chatWord = chatWord.replace(word, reverse);
- System.out.println(chatWord);
- } else {
- System.out.println("error");
- break;
- } break;
- case "InsertSpace":
- char insertChar = ' ';
- chatWord = insertSpace(chatWord, commands[1], insertChar);
- System.out.println(chatWord);
- }
- command = scan.nextLine();
- }
- System.out.println("You have a new text message: " + chatWord);
- }
- private static String insertSpace(String chatWord, String command, char insertChar) {
- int indexNum = Integer.parseInt(command);
- chatWord = chatWord.substring(0, indexNum) + insertChar + chatWord.substring(indexNum);
- // System.out.println(chatWord);
- return chatWord;
- }
- private static String reverse(String word) { // 1-vi metod
- String result = "";
- for (int i = word.length() - 1; i >= 0; i--) {
- result += word.charAt(i);
- }
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement