-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStrOccrOth.java
35 lines (27 loc) · 1.03 KB
/
StrOccrOth.java
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
/* Find the locations and count of occurrences of a string in
another string */
import java.util.Scanner;
class StrOccrOth
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the main string : ");
String mainString = sc.nextLine();
System.out.print("Enter the search string : ");
String searchString = sc.nextLine();
int count = 0;
int index = mainString.indexOf(searchString);
System.out.println("Locations of occurrences : ");
while(index != -1)
{
System.out.println("Index : "+index);
count++;
index = mainString.indexOf(searchString, index+1);
}
if(count > 0)
System.out.println("The search string ' "+searchString+" ' occurs "+count+" times in the main string.");
else
System.out.println("The search string ' "+searchString+" ' does not occur in the main string.");
}
}