Search pattern in a string, return all offset index - Java Data Structure

Java examples for Data Structure:Search

Description

Search pattern in a string, return all offset index

Demo Code

class Main/*ww  w .j  ava 2  s .  c  o m*/
{
    public static void search(String text, String pattern)
    {
        int lengthText = text.length();
        int lengthPattern = pattern.length();

        for(int i = 0; i <= lengthText - lengthPattern; i++)
        {
            int j;

            for(j = 0; j < lengthPattern; j++)
                if(text.charAt(i + j) != pattern.charAt(j))
                    break;

            if(j == lengthPattern)
               System.out.println("Pattern found at " + (i + 1));
        }
    }

    public static void main(String[] args)
    {
       String text = "namanchamanbomanamansanam";
       String pattern = "aman";
       search(text, pattern);
    }
}

Related Tutorials