Didart

Text Filter

Nov 10th, 2021
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.83 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class TextFilter {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.  
  7.         String[] bannedWordsArr = scanner.nextLine().split(", ");
  8.         String text = scanner.nextLine();
  9.  
  10.         for (String bannedWord : bannedWordsArr) {
  11.             if (text.contains(bannedWord)) {
  12.                 String replacementWord = repeatString("*", bannedWord.length());
  13.                 text = text.replace(bannedWord, replacementWord);
  14.             }
  15.            
  16.         }
  17.         System.out.println(text);
  18.     }
  19.  
  20.     public static String repeatString(String word, int count) {
  21.         String[] repeatArr = new String[count];
  22.         for (int i = 0; i < count; i++) {
  23.             repeatArr[i] = word;
  24.         }
  25.         return String.join("", repeatArr);
  26.     }
  27. }
  28.  
Advertisement
Add Comment
Please, Sign In to add comment