package com.pr3.bsbo_08_19.TimofeyKondakov.task11;

import java.util.Scanner;

public class Task11 {
    public void Ex11() {
//        Дан фрагмент текста. Заменить некоторые слова в абзаце на
//        звездочки, кроме первой и последней буквы слова (например замена
//                на звездочки в слове работа = р****а), в конце вывести количество
//        зацензуренных слов, отсортированных по частоте встрече в тексте

        Scanner in = new Scanner(System.in);

        System.out.println("Enter text: ");

        int length = 0;
        String str = in.nextLine();
        String[] words = str.split(" ");
        System.out.println("Enter count censored words: ");

        try {
            length = in.nextInt();
        } catch (Exception e){
            System.out.println(e.getMessage());
        }

        int[] countCensoredWords = new int[length];
        String[] censored = new String[length];
        String[] censoredWords = new String[length];

        for(int i = 0; i < censored.length; i++) {
            System.out.println("Enter censored word on number" + (i+1) + ": ");
            censored[i] = in.nextLine();
            if(i == 0)
                censored[i] = in.nextLine();
        }

        for (int i = 0; i < words.length; i++) {
            for(int j = 0; j < censored.length; j++){
                if (words[i].equals(censored[j])) {
                    words[i] = words[i].replaceAll("\\B\\W\\B", "*");
                    countCensoredWords[j]++;
                    censoredWords[j] = words[i];
                }
            }
        }
        System.out.println(String.join(" ", words));
        for(int i = 0; i < censored.length; i++){
            System.out.println("Word " + censoredWords[i] + " count: " + countCensoredWords[i]);
        }
    }
}