// Program 65: Delete a Specified Occurrence of a Word import java.util.*; class DeleteWordOccurrence { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String s = sc.nextLine().trim(); if (!s.matches("[A-Z ]+[.?!]")) { System.out.println("INVALID INPUT"); return; } char end = s.charAt(s.length() - 1); String body = s.substring(0, s.length() - 1).trim().replaceAll(" +", " "); String target = sc.nextLine().trim(); int position = Integer.parseInt(sc.nextLine().trim()); String[] words = body.split(" "); StringBuilder result = new StringBuilder(); int count = 0; for (String word : words) { if (word.equals(target)) { count++; } if (!(word.equals(target) && count == position)) { if (result.length() > 0) { result.append(' '); } result.append(word); } } if (count < position || position < 1) { System.out.println("WORD OCCURRENCE NOT FOUND"); } else { System.out.println(result + String.valueOf(end)); } } }