fork download
  1. import java.util.*;
  2. class RemoveWord_ISC2014
  3. {
  4. public static void main (String args[])
  5. {
  6. Scanner sc = new Scanner(System.in);
  7.  
  8. System.out.print("Enter a sentence : ");
  9. String s = sc.nextLine();
  10. s = s.toUpperCase();
  11. int l = s.length();
  12. char last = s.charAt(l-1); // Extracting the last character
  13.  
  14. /* Checking whether the sentence ends with '.' or '?' or not */
  15. if(last != '.' && last != '?' && last != '!')
  16. {
  17. System.out.println("Invalid Input. End a sentence with either '.', '?' or '!' only");
  18. }
  19. else
  20. {
  21. StringTokenizer str = new StringTokenizer(s," .?!");
  22. int c = str.countTokens();
  23. String w="",ans = "";
  24. System.out.print("Enter the word to delete : ");
  25. String del = sc.next();
  26. System.out.print("Enter the word position is the sentence : ");
  27. int x = sc.nextInt();
  28.  
  29. if(x<1 || x>c) // Checking whether integer inputted is acceptable or not
  30. {
  31. System.out.println("Sorry! The word position entered is out of range");
  32. }
  33. else
  34. {
  35. for(int i=1; i<=c; i++)
  36. {
  37. w = str.nextToken();
  38. /* Skipping if the word to delete and the position matches */
  39. if(w.equals(del)==true && i == x)
  40. continue;
  41. ans = ans + w + " ";
  42. }
  43. System.out.print("Output : "+ans.trim()+last);
  44. }
  45. }
  46. }
  47. }
  48.  
Success #stdin #stdout 0.07s 4386816KB
stdin
AS YOU    SOW, SO   SO YOU REAP.
SO
4
stdout
Enter a sentence : Enter the word to delete : Enter the word position is the sentence : Output : AS YOU SOW, SO YOU REAP.