-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFirstWordOccurence.java
More file actions
57 lines (49 loc) · 1.02 KB
/
Copy pathFirstWordOccurence.java
File metadata and controls
57 lines (49 loc) · 1.02 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
50
51
52
53
54
55
56
57
//29.Write a JAVA program to find first occurrence of a word in a given string.
import java.util.*;
public class FirstWordOccurence
{
public static void main(String x[])
{
Scanner ab=new Scanner(System.in);
System.out.println("Enter the String Array ");
String str=ab.nextLine();
/*
String s[]=new String[8];
for(int i=0; i<s.length; i++)
{
s[i]=ab.nextLine();
}
*/
System.out.println("Enter the Word to Find ");
String a=ab.nextLine();
int index=FindIndexOfWord(str,a);
if(index!=-1){
System.out.println("Word Present at Idex "+index);
}
else{
System.out.println("Not present ");
}
/*
int index=-1;
for(int i=0; i<s.length; i++)
{
if(s[i].equals(a))
{
index=i;
break;
}
}
if(index!=-1)
{
System.out.println(a+" is Present At the Index : "+index);
}
else
{
System.out.println("Word not Present");
}
*/
}
public static int FindIndexOfWord(String str,String word){
return str.indexOf(word);
}
}