fork download
  1. import java.util.ArrayList;
  2. import java.util.Map;
  3.  
  4. class Main {
  5.  
  6. public static void main(String[] args) {
  7. String text = "We have word1 to word6 and they are censored.";
  8. String result = asteriskCensor(text);
  9. System.out.println(result);
  10. }
  11.  
  12. private static String asteriskCensor(String text) {
  13. String[] wordList = text.split("\\s+");
  14. String result = "";
  15. ArrayList<String> badWords = new ArrayList<String>();
  16. badWords.add("word1");
  17. badWords.add("word2");
  18. badWords.add("word3");
  19. badWords.add("word4");
  20. badWords.add("word5");
  21. badWords.add("word6");
  22.  
  23. ArrayList<String> wordFix = new ArrayList<String>();
  24. wordFix.add("w*rd1");
  25. wordFix.add("w*rd2");
  26. wordFix.add("w*rd3");
  27. wordFix.add("w*rd4");
  28. wordFix.add("w*rd5");
  29. wordFix.add("w*rd6");
  30.  
  31. int index = 0;
  32. for (String word : wordList) {
  33. if (badWords.contains(word)) {
  34. wordList[index] = wordFix.get(badWords.indexOf(word));
  35. }
  36. index++;
  37. }
  38. for (String i : wordList) {
  39. result += i + ' ';
  40. }
  41. return result;
  42. }
  43. }
  44.  
Success #stdin #stdout 0.1s 52816KB
stdin
Standard input is empty
stdout
We have w*rd1 to w*rd6 and they are censored.