ASSIGNMENT 29: (05.11.
2024)
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 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 a part of the sentence along with its
posi on number, and delete the word and display the sentence.
Test your program for the following 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 SHALL YOU REAP SO.
WORD TO BE DELETED: SO
WORD POSITION IN THE SENTENCE: 8
OUTPUT: AS YOU SOW, SO SHALL YOU REAP.
Example 3
INPUT: STUDY WELL###
OUTPUT: INVALID INPUT
Algorithm:
1. Class Defini on and Method Setup
Class Remove: Contains the main logic and helper methods for sentence
valida on, space removal, and word dele on.
2. Method: check(String sen)
Purpose: Validates if the sentence ends with a valid punctua on mark and
if the characters are in uppercase (except spaces and punctua on).
Steps:
1. Ini alize pun as a string containing the valid punctua on marks (., ?,
!).
2. Check if the last character of the sentence (sen.charAt(sen.length() -
1)) is one of the valid punctua on marks using indexOf.
If the character is not in the valid punctua on string, print an
error message and return false.
3. Iterate through the sentence (excluding the last character) and check
if each character is either uppercase or a space.
If any character is not uppercase (and is not a space or
punctua on), print an error message and return false.
4. If both condi ons are sa sfied, return true.
3. Method: removeSpaces(String sen)
Purpose: Removes extra spaces between words and ensures only a single
space exists between them.
Steps:
1. Create a StringTokenizer object (ob) to split the sentence into tokens
(words).
2. Ini alize n_sen1 with the first token from the StringTokenizer. This
handles the case where the sentence might start with extra spaces.
3. For each subsequent token, append it to n_sen1 with a single space
in between.
4. Return the cleaned-up sentence without extra spaces.
4. Method: deleteWord(String sentence, String word, int pos)
Purpose: Deletes a specific word from the sentence at the given posi on,
handling punctua on correctly.
Steps:
1. Ini alize a StringTokenizer object (ob) to split the sentence into
words.
2. Ini alize n_sen2 as an empty string to store the modified sentence
and a counter w_count to track the word posi on.
3. Iterate through each token (word) in the sentence:
For each word, remove any punctua on by checking each
character of the word and building a new word (c_Word) that
contains only the alphabe c characters.
Compare the posi on (w_count) with the specified posi on
(pos). If they match and the word (c_Word) matches the
specified word (case-insensi ve), skip adding this word to the
new sentence (n_sen2), effec vely dele ng it.
If the word does not need to be deleted, append it to n_sen2
with its original punctua on.
4. A er finishing the loop, check if the word was successfully deleted. If
not, print an error message ("INVALID INPUT: Posi on does not
match or word not found at given posi on.") and terminate the
program.
5. Return the modified sentence with the word deleted.
5. Main Method
Input and Ini aliza on:
1. Create a Scanner object (sc) to take user input.
2. Prompt the user to input a sentence and store it in the sen variable.
Trim any leading or trailing spaces from the sentence.
3. Extract the punctua on mark at the end of the sentence (pun =
sen.substring(sen.length() - 1)).
Sentence Valida on:
1. Call the check(sen) method to validate the sentence.
2. If the sentence is invalid, return from the main method (exit the
program).
Removing Extra Spaces:
1. Call removeSpaces(sen.substring(0, sen.length() - 1)) to clean the
sentence (excluding the punctua on) by removing extra spaces.
Prompt for Word Dele on:
1. Prompt the user to enter the word to be deleted and the posi on of
the word in the sentence. Convert the word to uppercase to ensure
case-insensi vity.
Word Dele on:
1. Call deleteWord(n_sen1, word, pos) to remove the specified word
from the sentence at the given posi on.
Output:
1. Print the modified sentence (n_sen2) along with the punctua on
mark (pun) at the end.
Closing Resources:
1. Close the Scanner object to release resources.
Variable Listing Table:
Variable Type Scope Purpose
sen String main method Stores the input
sentence entered
by the user.
pun String main method Stores the
punctuation
character at the end
of the sentence.
n_sen1 String main and Holds the sentence
deleteWord without extra
spaces.
n_sen2 String deleteWord Stores the sentence
after deleting the
specified word.
word String main method The word the user
wants to delete
from the sentence.
pos int main method The position of the
word to delete.
w_count int deleteWord Counter to track
method the word position
in the sentence.
c_word String deleteWord Temporarily holds
method each word in the
sentence for
comparison.
c_Word String deleteWord Stores the current
method word without
punctuation for
comparison.
wordDeleted boolean deleteWord Tracks if the
method specified word has
been deleted
successfully.
sc Scanner main method Reads input values
from the user.
pun String check and main Defines the valid
methods punctuation marks
(".", "?", "!").
Method Listing Table:
Method Prototype Purpose
check public static Validates the sentence
boolean format (uppercase and
check(String sen) punctuation).
removeSpaces public static Removes extra spaces
String between words in the
removeSpaces(St sentence.
ring sen)
deleteWord public static Deletes the specified word at
String the given position.
deleteWord(Stri
ng sentence,
String word, int
pos)
main public static void Drives the program with user
main(String[] input, using other methods to
args) manipulate the sentence.
Class Listing Table:
Class Description
Remove Contains methods to check sentence format,
remove spaces, and delete a specified word.
OUTPUT: