Showing posts with label Pattern Matching using Java. Show all posts
Showing posts with label Pattern Matching using Java. Show all posts

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...