SUNDAY, 12 JULY 2026
Guide For School logo Guide For SchoolStudy Guide For Students On Java Programming
Physics | Chemistry | Mathematics
ICSE | ISC | CBSE
Guide For School logo Guide For SchoolICSE and ISC Resources

Remove Word from Sentence Program in Java and Python

20 February 2014

Remove word from sentence program with algorithm, explanation, Java solution and simple Python solution for ISC students.

Question:

Write a program to accept a sentence which may be terminated by '.', '?' or '!' only. Any other terminating character makes the input invalid. The words may be separated by more than one blank space and are in uppercase.

Reduce extra blank spaces between words to a single blank space. Then accept a word and its position number, delete that word from the specified position and display the sentence.

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

Algorithm:

Step 1: Start.

Step 2: Accept the sentence from the user.

Step 3: Check the last character of the sentence.

Step 4: If the last character is not period, question mark or exclamation mark, display INVALID INPUT and stop.

Step 5: Remove the terminating punctuation mark and store it separately.

Step 6: Use tokenization to split the sentence into words, automatically removing extra blank spaces.

Step 7: Accept the word to delete and the position number.

Step 8: Read each word in order using a loop and a position counter.

Step 9: Skip the word only when both the position and word match the given input.

Step 10: Append all other words to the output sentence with a single blank space.

Step 11: Attach the original punctuation mark at the end and display the final sentence.

Step 12: Stop.

Explanation:

The program removes a word only when both the word and its position match the user’s input. It begins by converting the sentence to uppercase so that comparison becomes case-insensitive. The last character is checked to ensure that the sentence ends with one of the allowed terminators: full stop, question mark or exclamation mark. If the terminator is invalid, the program displays invalid input and does not continue with deletion.

The sentence is split into words using a regular expression that treats punctuation and blanks as separators. The resulting array word stores the words without the final punctuation. The number of words is stored in c, and the entered position x is checked against this count. If the position is outside the valid range, no deletion is attempted.

Inside the loop, the program checks two things for every word: whether the word equals the word to delete, and whether its index matches the required position. Since array indexes start from 0, the required index is x - 1. Only when both conditions are true does the loop skip that word using continue. All other words are added to ans. This prevents accidental deletion of the same word if it appears at another position.

Java Program:

Java
/**
* 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.Scanner;
class RemoveWord_ISC2014
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter any sentence : "); // Inputting the sentence
        String s = sc.nextLine();
        s = s.toUpperCase(); // Converting the sentence into Upper Case
        int l = s.length();

        String ans=""; // String variable to store the final result
        char last = s.charAt(l-1); // Extracting the last character

        /* Checking whether the sentence ends with '.', '?' or a '!' or not */
        if(last == '.' || last == '?' || last == '!')
        {
            String word[]=s.split("[.?! ]+"); // Saving the words in an array using split()
            int c = word.length; // Finding the number of words

            System.out.print("Enter the word to delete : ");
            String del = sc.nextLine();
            del = del.toUpperCase();

            System.out.print("Enter the word position in 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=0; i<c; i++)
                {
                    /* Skipping if the word to delete and the position matches */
                    if(word[i].equals(del)==true && i == x-1)
                    continue;
                    ans = ans + word[i] + " ";
                }
                System.out.print("Output : "+ans.trim()+last);
            }
        }

        else
        {
            System.out.println("Invalid Input. End a sentence with either '.', '?' or '!'");
        }
    }
}

Equivalent Python Program:

Python
# Read the text input and split or scan it according to the question requirement.
# Loops process each word/character and update counters or result strings.
# Display the final text result after sorting, checking or rearranging is complete.

sentence = input("Enter a sentence: ")
last = sentence[len(sentence) - 1]

if last != '.' and last != '?' and last != '!':
    print("INVALID INPUT")
else:
    text = sentence[0:len(sentence) - 1]
    word_delete = input("Enter the word to delete: ")
    pos = int(input("Enter the word position in the sentence: "))

    words = text.split()
    result = ""

    for i in range(len(words)):
        current_pos = i + 1
        if current_pos == pos and words[i] == word_delete:
            continue
        result = result + words[i] + " "

    result = result.strip() + last
    print(result)

Output:

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

Leave a Reply

Your email address will not be published. Comments are reviewed before appearing publicly.

Send a comment or correction

Study smarter

Everything you need for ICSE and ISC Computer

Programs, revision notes, solved papers and practical guidance—organized for quick study.

Browse all resources →