WAP to accept a sentence which may be terminated by
either , ; ? . or !(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:
(i) Accept the sentence and reduce all extra blank spaces between
two words to a single blank space.
(ii) 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 distance.
Example:
Input: AS YOU SOW,SO SO YOU REAP.
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
import java.util.*;
public class code
{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter a sentence:");
String m = sc.nextLine();
char ch = m.charAt(m.length()-1);
m = m.substring(0,m.length()-1);
if(".;?!".indexOf(ch)!=-1)
{
StringTokenizer st = new StringTokenizer(m);
System.out.println("Enter the word you want to delete:");
String word = sc.next().trim();
System.out.println("Enter the position:");
int pos = sc.nextInt();
int c=0;
String result=" ";
while(st.hasMoreTokens())
{
String g=st.nextToken().trim();
if(c==pos)
{
result=result+"";
}
else{
result=result+g+" ";
}
c=c+1;
}
System.out.println("The required sentence: "+result);
}
}
}