Wednesday, April 21, 2010

Brute Force Algorithm(Internet algorithm)

import java.io.*;
class bf_fun
{
    int bf(String t,String p)
    {
        int n=t.length();
        int m=p.length();
        for(int i=0;i<=n-m;i++)
        {
            int j=0;
            while(j                j++;
            if(j==m)
            return i+1;
        }
        return -1;
    }
}

class bf
{
    public static void main(String args[])
    throws IOException
    {
        InputStreamReader input=new InputStreamReader(System.in);
        BufferedReader obj=new BufferedReader(input);
        System.out.println("Enter the string:");
        String s=obj.readLine();
        System.out.print("Enter the substring:");
        String ts=obj.readLine();
        bf_fun f=new bf_fun();
        int n=f.bf(s,ts);
        if (n!=-1)
            System.out.println("Pattern matched at position "+n);
           
        else
            System.out.println("No pattern matching");   
    }
}
/*

OUTPUT
Enter the string:
WHAT IS UR NAME
Enter the substring:IS
Pattern matched at position 6

*/

0 comments:

Post a Comment

 
ShareThis

Visitor

Website counter
Copyright 2009 Code's. Powered by Blogger
Blogger Templates created by Deluxe Templates
Wordpress by Wpthemescreator
Blogger Showcase