Advertisement
BERKYT

LABA 3 KSIPO t11

Nov 17th, 2020 (edited)
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.01 KB | None | 0 0
  1. package com.pr3.bsbo_08_19.TimofeyKondakov.task11;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Task11 {
  6.     public void Ex11() {
  7. //        Дан фрагмент текста. Заменить некоторые слова в абзаце на
  8. //        звездочки, кроме первой и последней буквы слова (например замена
  9. //                на звездочки в слове работа = р****а), в конце вывести количество
  10. //        зацензуренных слов, отсортированных по частоте встрече в тексте
  11.  
  12.         Scanner in = new Scanner(System.in);
  13.  
  14.         System.out.println("Enter text: ");
  15.  
  16.         int length = 0;
  17.         String str = in.nextLine();
  18.         String[] words = str.split(" ");
  19.         System.out.println("Enter count censored words: ");
  20.  
  21.         try {
  22.             length = in.nextInt();
  23.         } catch (Exception e){
  24.             System.out.println(e.getMessage());
  25.         }
  26.  
  27.         int[] countCensoredWords = new int[length];
  28.         String[] censored = new String[length];
  29.         String[] censoredWords = new String[length];
  30.  
  31.         for(int i = 0; i < censored.length; i++) {
  32.             System.out.println("Enter censored word on number" + (i+1) + ": ");
  33.             censored[i] = in.nextLine();
  34.             if(i == 0)
  35.                 censored[i] = in.nextLine();
  36.         }
  37.  
  38.         for (int i = 0; i < words.length; i++) {
  39.             for(int j = 0; j < censored.length; j++){
  40.                 if (words[i].equals(censored[j])) {
  41.                     words[i] = words[i].replaceAll("\\B\\W\\B", "*");
  42.                     countCensoredWords[j]++;
  43.                     censoredWords[j] = words[i];
  44.                 }
  45.             }
  46.         }
  47.         System.out.println(String.join(" ", words));
  48.         for(int i = 0; i < censored.length; i++){
  49.             System.out.println("Word " + censoredWords[i] + " count: " + countCensoredWords[i]);
  50.         }
  51.     }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement