11 June 2018

find the index of the second string in first substring






input: string1->guru build new blog
           string2->build
output: (print index which string2 found in string1)
               5,9


  import java.util.*;
public class NewClass {
     void print(int i,int j)
     {
         if(i==0 && j==0)
             System.out.println("not found");
         else
         System.out.println(i+","+j);
     }
    
     void sub(String s1,String s2,int l1,int l2)
     {        int c=0;
         for(int i=0;i<=(l1-l2);i++)
         {
             String k=s1.substring(i,i+l2);
             if(k.equals(s2))
             {
                 c=1;
                 print(i,i+l2);
             }
         }
            if(c==0)
             print(0,0);
     }
    
    
public static void main(String[] args)
     {
         String s1="guru build new blog";
         String s2="build";
         NewClass ob=new NewClass();
         ob.sub(s1,s2,s1.length(),s2.length());
     }
    
            
}

No comments:

Post a Comment