Thursday 7 February 2013

Pattern Matching implementation in JAVA implementing Directory

Pattern Matching implementation in JAVA implementing Directory

import java.io.*;
class Mat
{
  public String patt;
  public int flg=0;
  public Mat(String p)
  {
      patt=p;
  }
  public void chk(File f) throws IOException
  {
        if (f.isDirectory())
        {
            File[] lf = f.listFiles();
            for (int i = 0; i < lf.length; i++)
            {
            chk(lf[i]);
            }
        }
        else
            srch(f);
  }
  public void srch(File f) throws IOException
  {
          FileInputStream fstream = new FileInputStream(f);
        DataInputStream in = new DataInputStream(fstream);
        BufferedReader brd = new BufferedReader(new InputStreamReader(in));
        String strLine;
        int l=0;
        while ((strLine = brd.readLine()) != null) 
            {
            l=l+1;
                int n=strLine.length();
                int m=patt.length();
                char p;
                char t;
                int s=0;
                int    i=0;
                int j=0;
                while(i<n && s!=1)
                {
                    t=strLine.charAt(i);
                    p=patt.charAt(j);
                    if(p==t)
                    {
                        i++;
                        j++;
                        if(j>=m)
                        {
                            s=1;
                        }
                    }
                    else
                    {
                        j=0;
                        i++;
                    }
                }
                if(s==1)
                {
                    System.out.println("Found Pattern at Line "+ l+" in file" +f);
                    flg=flg+1;
                }
                s=0;
            }
  }
}
  
class Pat
{
    public static void main(String[] args) throws IOException
    {
        String patt;
        File f=new File("D:\\Sameer\\Pat.java");
        InputStreamReader isr = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(isr);
        System.out.println("enter pattern");
        patt=br.readLine();
        File f1=f.getParentFile();
        Mat m=new Mat(patt);
        m.chk(f1);
        System.out.println("Pattern Found "+m.flg+" times");
    }
}
       

Monday 4 February 2013

Pattern Matching using Java

import java.io.*;
class Pat
{
    public static void main(String[] args) throws IOException
    {
        String patt,strLine;
        int l=0,f=0,count=0,fc=1;
        InputStreamReader isr = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(isr);
        System.out.println("enter pattern");
        patt=br.readLine();
        while(fc<6)
        {
        FileInputStream fstream = new FileInputStream("textfile"+fc+".txt");
                DataInputStream in = new DataInputStream(fstream);
                BufferedReader brd = new BufferedReader(new InputStreamReader(in));
        while ((strLine = brd.readLine()) != null)  
            {
            l=l+1;
                int n=strLine.length();
                int m=patt.length();
                char p;
                char t;
                int s=0;
                int    i=0;
                int j=0;
                while(i<n && s!=1)
                {
                    t=strLine.charAt(i);
                    p=patt.charAt(j);
                    if(p==t)
                    {
                        i++;
                        j++;
                        if(j>=m)
                        {
                            s=1;
                        }
                    }
                    else
                    {
                        j=0;
                        i++;
                    }
                }
                if(s==1)
                {
                    count++;
                    System.out.println("Found Pattern in File textfile"+fc+" at Line "+ l+" at position "+(i-j+1));
                    f=1;
                }
                s=0;
            }
            if(f==0)
                    System.out.println("Pattern not found ");
            else
                    System.out.println("Pattern count= "+count);
            fc++;
        }
    }
}


Note: Before executing this program create 5 text files with name textfile1.txt, textfile2.txt, textfile3.txt, textfile4.txt, textfile5.txt.

Best geography books for UPSC prelims, mains

This post is intended to clear the confusion that prevails among the aspirants over how to prepare for UPSC geography and the best books f...