Advertisement
danisun18

SecretChat

Aug 4th, 2020
576
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.04 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class SecretChat {
  4.     public static void main(String[] args) {
  5. // 1 greshka  87 / 100
  6.  
  7.         Scanner scan = new Scanner(System.in);
  8.         String chatWord = scan.nextLine();
  9.         String command = scan.nextLine();
  10.         while (!command.equals("Reveal")) {
  11.             String[] commands = command.split("\\:\\|:");
  12.             switch (commands[0]) {
  13.                 case "ChangeAll":
  14.                     String searchFor = commands[1];
  15.                     String replaceWith = commands[2];
  16.                     chatWord = chatWord.replaceAll(searchFor, replaceWith);
  17.                     System.out.println(chatWord);
  18.                     break;
  19.                 case "Reverse":
  20.                     String word = commands[1];
  21.                     if (chatWord.contains(word)){
  22.                         String reverse = reverse(word); //1-vi metod
  23.                         chatWord = chatWord.replace(word, reverse);
  24.                         System.out.println(chatWord);
  25.                     } else {
  26.                         System.out.println("error");
  27.                         break;
  28.                     } break;
  29.                 case "InsertSpace":
  30.                     char insertChar = ' ';
  31.                     chatWord = insertSpace(chatWord, commands[1], insertChar);
  32.                     System.out.println(chatWord);
  33.             }
  34.             command = scan.nextLine();
  35.         }
  36.             System.out.println("You have a new text message: " + chatWord);
  37.     }
  38.  
  39.  
  40.     private static String insertSpace(String chatWord, String command, char insertChar) {
  41.         int indexNum = Integer.parseInt(command);
  42.         chatWord = chatWord.substring(0, indexNum) + insertChar + chatWord.substring(indexNum);
  43.        // System.out.println(chatWord);
  44.         return chatWord;
  45.  
  46.     }
  47.  
  48.     private static String reverse(String word) {  // 1-vi metod
  49.         String result = "";
  50.         for (int i = word.length() - 1; i >= 0; i--) {
  51.             result += word.charAt(i);
  52.         }
  53.         return result;
  54.     }
  55. }
  56.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement