Вы находитесь на странице: 1из 7

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 a sentence and reduce all the extra blank space between 2 words to a
single blank space.
b) Accept a word from the user which is a part of the sentence along with its
position number
and delete the word and display the sentence.
Test your program for the following data and some random data:
Example 1
INPUT:

ENTER WORD
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:

ENTER WORD
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:

ENTER WORD
STUDY WELL ##.
OUTPUT: INVALID INPUT.

-_____________________________________________________-

ALGORITHM
Step-1Start
Step-2A class name DeleteWord is created.
Step-3A main() function is created.
Step-4An object obj of class DeleteWord is created.
Step-5displayENTER WORD
Step-6Input s
Step-7check if((obj.isUpper(s)==1)&&(obj.isComplete(s)==1))
Step-8if true then {
displayWORD TO BE DELETED
Step-9Input dl
Step-10displayWORD POSITION IN SENTENCE
Step-11Input wp
Step-12pass s ,dl and wp to function DELETE using object obj }
Step-13if false then display INVALID INPUT
Step-14end of main() function
Step-15A function isUpper(String s) of returntype int is created
Step-16A variable ab of character datatype is created to extract characters
Step-17A flag variable f of datatype int is created.
Step-18for(int i=0;i<s.length()-1;i++) {
Step-19store s.charAt(i) in ab
Step-20check if((ab>=65&&ab<=90)||(ab==',')||(ab==32))
Step-21store f=1
Step-22if false then
{ f--;
break; } }
Step-23return f
Step-24end of isUpper() function
Step-25A function isComplete(String s) of returntype int is created
Step-26Store s.charAt(s.length()-1) in ab
Step-27check if(ab=='.'||ab=='!'||ab=='?')
Step-28if true then return 1
Step-29if false then return 0
Step-30end of isComplete() function
Step-31A function DELETE(String s,String dl,int wp) is created
Step-32A counter variable c=0 of integer datatype is created
Step-33A variable s1 of string datatype is created
Step-34A variable ab of character datatype is created
Step-35for(int i=0;i<s.length();i++) {
Step-36store s.charAt(i) in ab
Step-37check if((ab==32)||ab=='.'||ab==,)
Step-38if true then
{ ++c;
Step-39check if((s1.compareTo(dl)==0)&&(c==wp))
Step-40store in s1

-_____________________________________________________-

Step-41if false then display s1+ab


Step-42store in s1 }
Step-43if false then s1+ab in s1
Step-44end of DELETE() function
Step-45end of class
Step-46Stop

-_____________________________________________________-

PROGRAM
import java.io.*;
class DeleteWord
{
public static void main(String args[])throws IOException
{
BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
DeleteWord obj=new DeleteWord();
System.out.println("ENTER WORD");
String s=stdin.readLine();
if((obj.isUpper(s)==1)&&(obj.isComplete(s)==1))
{
System.out.print("WORD TO BE DELETED:");
String dl=stdin.readLine();
System.out.print("WORD POSITION IN SENTENCE:");
int wp=Integer.parseInt(stdin.readLine());
obj.DELETE(s,dl,wp);
}
else
System.out.println("INVALID INPUT");
}
int isUpper(String s)
{
char ab;
int f=1;
for(int i=0;i<s.length()-1;i++)
{
ab=s.charAt(i);
if((ab>=65&&ab<=90)||(ab==',')||(ab==32))
f=1;
else
{
f--;
break;
}
}
return f;
}
int isComplete(String s)
{
char ab=s.charAt(s.length()-1);
if(ab=='.'||ab=='!'||ab=='?')

-_____________________________________________________-

return 1;
else
return 0;
}
void DELETE(String s,String dl,int wp)
{
int c=0;
String s1="";
char ab;
for(int i=0;i<s.length();i++)
{
ab=s.charAt(i);
if((ab==32)||ab=='.'||ab==',')
{
++c;
if((s1.compareTo(dl)==0)&&(c==(wp)))
s1="";
else
System.out.print(s1+ab);
s1="";
}
else
s1=s1+ab;
}
}
}

-_____________________________________________________-

VARIABLE DESCRIPTION
Datatype
String

Variable Name
s

Description
To store a sentence

String

dl

To delete a word

int

wp

To store word position

char

ab

To extract characters

int

Tripping variable

int

Looping variable

INPUT AND OUTPUT


-_____________________________________________________-

1.INPUT/OUTPUT
ENTER WORD
AS YOU SOW,SO SO YOU REAP.
WORD TO BE DELETED:SO
WORD POSITION IN SENTENCE:4
AS YOU SOW,SO YOU REAP.
2.INPUT/OUTPUT
ENTER WORD
A MORNING WALK IS A IS BLESSING FOR THE WHOLE DAY.
WORD TO BE DELETED:IS
WORD POSITION IN SENTENCE:6
A MORNING WALK IS A BLESSING FOR THE WHOLE DAY.
3.INPUT/OUTPUT
ENTER WORD
GOKU ID A GOD
INVALID INPUT

-_____________________________________________________-

Вам также может понравиться