forked from NischayKG/My_Java_Programs_of_Strings
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcount_substring.java
More file actions
43 lines (39 loc) · 1.05 KB
/
count_substring.java
File metadata and controls
43 lines (39 loc) · 1.05 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
//Program to Count the Occurence of a Substring in String
import java.util.*;
class count_substring
{
public static int check(String str,String str2)
{
int count=0;
int l1=str.length();
int l2=str2.length();
int j=0;
for(int i=0;i<l1;i++)
{
if(str.charAt(i)==str2.charAt(j))
{
j++;
if(j==l2)
{
count++;
j=0;
}
}
else
{
j=0;
}
}
return count;
}
public static void main(String arr[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the string");
String str=new String(sc.nextLine());
System.out.println("Enter the Substring");
String str2=new String(sc.nextLine());
int res=check(str,str2);
System.out.println("The number of times substring occured in a string is = "+res);
}
}