Showing posts with label Brute Force Pattern Matching. Show all posts
Showing posts with label Brute Force Pattern Matching. Show all posts

Thursday, April 14, 2011

Brute Force Pattern Matching


import java.util.*;
class Pattern
{
                 int brute(String text,String pattern)
                {
                                int n = text.length();    //n is the length of text
                                int m = pattern.length(); // m is length of pattern
                                int j;
                                for(int i=0; i <= (n-m); i++)
                                {  
                                                j = 0;
                                                while ((j < m) && (text.charAt(i+j) == pattern.charAt(j)) )  
                                                j++;  
                                                if (j == m)  
                                                                return i;   // match at i
                                }
                                return -1;   // no match
                }// end of brute()

}



public class BruteForce
{
                public static void main(String args[])
                {