-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountOfWord.java
More file actions
62 lines (55 loc) · 1.2 KB
/
Copy pathCountOfWord.java
File metadata and controls
62 lines (55 loc) · 1.2 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
58
59
60
61
62
//32. Write a JAVA program to count occurrences of a word in a given string.
import java.util.*;
public class CountOfWord
{
public static void main(String x[])
{
Scanner ab=new Scanner(System.in);
System.out.println("Enter the Array of String ");
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 Occurrence ");
String word=ab.nextLine();
findCountOfWord(str,word);
/*
int count=0;
for(int i=0; i<s.length; i++)
{
if(s[i].equals(str))
{
count++;
}
}
if(count!=0)
{
System.out.println(str+" is present in the String is : "+count+" times");
}
else
{
System.out.println("Word not present is String ");
}
*/
}
public static void findCountOfWord(String str,String word){
int index=0;
int count=0;
//System.out.println("Count of Words in String : "+count);
while(index<str.length()){
index=str.indexOf(word,index);
if(index!=-1){
count++;
index=index+word.length();
}
else{
break;
}
}
System.out.println("Count of Words in String : "+count);
}
}