Advertisement
d1i2p3a4k5

13.Occurencences of strings

Oct 18th, 2014
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.56 KB | None | 0 0
  1. import java.util.*;
  2. import java.lang.*;
  3. class occurs
  4. {
  5.     public static void main(String args[])
  6.     {
  7.         Scanner t = new Scanner (System.in);
  8.         System.out.println("enter 1st string");
  9.         String s1 = t.next();
  10.         int n1 = s1.length();
  11.         System.out.println("enter the 2nd string ");
  12.         String s2 = t.next();
  13.         int n2 = s2.length();
  14.         int i,count=0;
  15.         String temp;
  16.         for(i=0;i<=n1-n2;i++)
  17.         {
  18.             temp = s1.substring(i,i+n2);
  19.             if(s2.equals(temp))
  20.             {
  21.                 count++;
  22.             }
  23.         }
  24.         System.out.println("Occurences of 2nd string in 1st string is "+count+" times");
  25.     }
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement