-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSentence.java
More file actions
49 lines (48 loc) · 1.33 KB
/
Sentence.java
File metadata and controls
49 lines (48 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import java.util.*;
public class Sentence
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("SENTENCE");
System.out.print("Sentence [string] ->");
String str=sc.nextLine();
char punc=' ';
if(str.indexOf('.')>0)
{
str=str.substring(0,str.indexOf('.'));
punc='.';
}
else if(str.indexOf('?')>0)
{
str=str.substring(0,str.indexOf('?'));
punc='?';
}
else if(str.indexOf('!')>0)
{
str=str.substring(0,str.indexOf('!'));
punc='!';
}
sc.close();
sc=new Scanner(System.in);
System.out.print("Word to delete [string] ->");
String word=sc.next();
sc.close();
sc=new Scanner(str);
String output="";
sc.useDelimiter(" ");int ctr=0;int k=0;
while(sc.hasNext())
{
String part=sc.next();
if(part.equals("")) continue;
ctr++;
if(part.equalsIgnoreCase(word)==false)
output=output+part+" ";
else
k=ctr;
}
output=output.trim();
System.out.println(output+((punc==' ')?"":punc));
System.out.println("Position of word deleted= "+k);
}
}