Thursday , 19 October 2017
Home » Class 12 » ISC 2014 [Question 3] Practical Paper Solved – Removing Words

ISC 2014 [Question 3] Practical Paper Solved – Removing Words

Click here to download the complete ISC 2014 Computer Science Paper 2 (Practical).

Question:

Write a program to accept a sentence which may be terminated by either ‘.’ ‘?’ or ‘!’ only. Any other character may be ignored. The words may be separated by more than one blank space and are in UPPER CASE.

Perform the following tasks:

(a)        Accept the sentence and reduce all the extra blank space between two words to
a single blank space.
(b)        Accept a word from the user which is part of the sentence along with its
position number and delete the word and display the sentence.

Test your program with the sample data and some random data:

Example 1

INPUT:          A    MORNING WALK IS A IS BLESSING FOR   THE  WHOLE DAY.

WORD TO BE DELETED: IS
WORD POSITION IN THE SENTENCE: 6

OUTPUT:      A MORNING WALK IS A BLESSING FOR THE WHOLE DAY.

Example 2

INPUT:          AS YOU    SOW, SO   SO YOU REAP.

WORD TO BE DELETED: SO
WORD POSITION IN THE SENTENCE: 4

OUTPUT:      AS YOU SOW, SO YOU REAP.

Example 3

INPUT:          STUDY WELL ##.

OUTPUT:      INVALID INPUT.

Programming Code:

/**
 * The class RemoveWord_ISC2014 inputs a sentence. It also inputs a word and an integer.
 * It then removes the word present at that position in the sentence
 * @author : www.guideforschool.com
 * @Program Type : BlueJ Program - Java
 * @Question Year : ISC Practical 2014 Question 3
 */
import java.util.*;
class RemoveWord_ISC2014
{           
    public static void main (String args[])
    {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter a sentence : ");
        String s = sc.nextLine();
        s = s.toUpperCase();
        int l = s.length();
        char last = s.charAt(l-1); // Extracting the last character

        /* Checking whether the sentence ends with '.' or '?' or not */
        if(last != '.' && last != '?' && last != '!')
        {
            System.out.println("Invalid Input. End a sentence with either '.', '?' or '!' only");
        }
        else
        {
            StringTokenizer str = new StringTokenizer(s," .?!");
            int c = str.countTokens();
            String w="",ans = "";
            System.out.print("Enter the word to delete : ");
            String del = sc.next();
            System.out.print("Enter the word position is the sentence : ");
            int x = sc.nextInt();

            if(x<1 || x>c) // Checking whether integer inputted is acceptable or not
            {
                System.out.println("Sorry! The word position entered is out of range");
            }
            else
            {      
                for(int i=1; i<=c; i++)
                {
                    w = str.nextToken();
                    /* Skipping if the word to delete and the position matches */
                    if(w.equals(del)==true && i == x)
                        continue;
                    ans = ans + w + " ";
                }
                System.out.print("Output : "+ans.trim()+last);
            }
        }
    }
}

Output:

1. Enter any sentence : A MORNING WALK IS A IS BLESSING FOR THE WHOLE DAY.

Enter the word to delete : IS
Enter the word position is the sentence : 6

Output : A MORNING WALK IS A BLESSING FOR THE WHOLE DAY.

2. Enter any sentence : STUDY WELL ##
OUTPUT : Invalid Input. End a sentence with either ‘.’, ‘?’ or ‘!’

Check Also

[Question 2] ISC 2017 Computer Practical Paper Solved – Quiz Result

Solution of Program 2 of ISC 2017 Computer Science Paper 2 (Practical) Exam. Java program to input the answers of each participant row-wise and calculate their marks

12 comments

  1. Sir , you did not check for extra blank space ?

  2. sir plz explain the function String word[]=s.split(“[.?! ]+”)

    • Hello Ritu,

      split() function splits a string into tokens (words) and returns the result in the form of a String array.
      Example: If String s = “Java For School”, then String w[]=s.split(” “); will save the words Java, For and School in the array w[]

      Just note: If there are 2 spaces in between any words or a fullstop followed by a space as in “Java is Fun. Said none”, the above code needs to be modified otherwise it will store null strings also in the array.
      Just use this syntax: String word[]=s.split(“[. ]+”);
      where ‘s’ is the sentence. ‘+’ denotes any possible combinations of the punctuators mentioned in the []. Similarly you can use other punctuation marks by putting them inside like this “[?!,;. ]+”

      Regards,
      guideforschool

  3. I worte this very program when our class teacher gave us this question … but he marked me worng saying that we cannot use the split function in ISC ..we have to use some other technique to extract the words with singular spaces in between them. Is this true ??
    can u post another solution where we can do the same operation without the split function ?

    • Your teacher did a mistake in marking you wrong. You can use any of the String class functions in ISC unless mentioned otherwise. If you go through the scope of syllabus, you will not find mentioned anywhere that you cannot use any specific String functions.

      There are other alternatives as well, you can use StringTokenizer class.

      Instead of these lines:

      String word[]=s.split("[.?! ]+"); // Saving the words in an array using split()
      int c = word.length; // Finding the number of words

      write the following:

      StringTokenizer str = new StringTokenizer(s,".?! ");
      int c = str.countTokens();
      String word[] = new String[c];
      for(int i=0; i<c; i++)
      {
      word[i] = str.nextToken();
      }
  4. Sir, I have written algorithm in a different way.. As in in small sentences as given in Sumatra Arora ‘s computer sci in Java.. But it is a bit different from others ..
    Will it make any difference ?

  5. Respected Sir,

    I have not given a condition for checking if the input position of word to be deleted is less than the length of the no of words i.e

    if(x<1 || x>c) // Checking whether integer inputted is acceptable or not

    and also i have not stored the words i extracted onto another new string rather I Sop -ed it and also added the end character via Sop.

    So the output is all good for all the sentences I tried. Will I lose marks since I did this way?
    Thank you.

Leave a Reply to rajannair Cancel reply

Your email address will not be published. Required fields are marked *