Java Program to Count the Occurences of Sub String
Get a String and Substring and count the occurrence of substring in string.
Sample Input 1:
entertainment
en
Sample Output 1:
2
Try your Solution
Strongly recommended to Solve it on your own, Don't directly go to the solution given below.
public class Hello
{
public static void main(String args[])
{
//Write your code here
}
}
Program or Solution
//Count the occurences of given substring in a String...
import java.util.*;
class Program
{
public static void main(String args[])
{
int i,k,count=0;
String full,half;
Scanner sc=new Scanner(System.in);
System.out.println("Enter The String:");
full=sc.next();
System.out.println("Enter The Sub-String:");
half=sc.next();
int len1=full.length();
int len2=half.length();
for(i=0;i<len1;i++)
{
int j=0,b=0;
if(full.charAt(i)==half.charAt(j))
{
for(k=i;j<len2;k++,j++)
{
if(full.charAt(k)!=half.charAt(j))
{
b=1;
break;
}
}
if(b==0)
{
count++;
}
}
}
System.out.println("The Count Is:"+count);
}
}
Program Explanation
Comments
Related Programs
- Java Program to Print the English Alphabets In Lower Case
- Java Program to Count The Occurences Of Character In a String
- Java Program to Compare two Strings are Equal
- Java Program to Concatenate two Strings
- Java Program to find the index of character in a String
- Java Program to find length of a string
- Java Program to convert lower case to upper case
- Java Program to Find the Location of Sub String in String
- Java Program to Check whethet given String is Palindrome
- Java Program to Reverse the Given String
- Java Program to Remove the Spaces in given String
- Java Program to Remove the Vowels in given String
- Java Program to insert space between every Characters
- Java Program to Convert Uppercase to Lowercase
- Java Program to Count the Vowels
coming Soon
coming Soon