Pages

Friday, October 25, 2013

BRUTE FORCE ALGORITHM IN JAVA.

/*
TOPIC: BRUTE FORCE ALGORITHM.

DATE:11/9/2012

*/
import java.io.*;
import javax.swing.*;
class StringMatching
{
BufferedReader br;
String t,pat,mid;
int pos,i;
PrintStream p;
StringMatching()
{
p=System.out;
br=new BufferedReader(new InputStreamReader(System.in));
}
void getData()
{
p.println("Enter the TEXT STRING: ");
t=JOptionPane.showInputDialog("Enter string","");
p.println("Enter the PATTERN :");
pat=JOptionPane.showInputDialog("Enter Pattern","");
p.println("pattern is: "+pat+" text is "+t);
}
void Bruteforce()
{
for(pos=0;pos<=(t.length()-pat.length());pos++)
{
mid=t.substring(pos,pos+pat.length());
if(pat.equals(mid))
{
p.println(t);
for(i=0;i<pos;i++)

p.print(" ");
p.println(pat);
p.println();

}
}
}
public static void main(String args[])throws Exception
{
StringMatching sm=new StringMatching();
sm.getData();
sm.Bruteforce();
}
}

/*
OUTPUT:
Enter the TEXT STRING:
Enter the PATTERN :
pattern is: ll  text is hello to all
hello to all
  ll

hello to all
          ll
*/

No comments:

Post a Comment