Friday, July 20, 2012

Boyer Moore Algorithm Using Bad Character Rule In C

         Do you want to publish source codes in your blog or web site as follows.
                              Visit Source Code Formatter

 #include <stdio.h>  
 #include <stdlib.h>  
 #include <string.h>  
 #include <errno.h>  
   
 int boyer_moore();  
   
 char pattern[256]={0};  
   
 int main (){  
   int i=0;  
   char filename[256] = {0};  
   //Get text file  
   printf("Enter path of file to read: ");  
      if (fgets(filename, sizeof(filename), stdin) == NULL){  
        fprintf(stderr, "Invalid input!\n");  
        exit(-1);  
      }  
   
   filename[strlen(filename) - 1] = '\0';  
   FILE *file = fopen(filename, "r");  
     if (!file){  
        fprintf(stderr, "Unable to open %s: %s\n",  
        filename, strerror(errno));  
        exit(-1);  
   }  
   //Get file size  
   fseek(file, 0L, SEEK_END);  
   int sz = ftell(file);  
   fseek(file, 0L, SEEK_SET);  
   //printf("size = %d",sz);  
   //create file size array  
   char buffer[sz];  
   for(i=0;i<sz;i++){  
     buffer[i]=0;  
   }  
   //Get pattern  
   printf("Enter pattern to check : ");  
      if (fgets(pattern, sizeof(pattern), stdin) == NULL){  
        fprintf(stderr, "Invalid input pattern!\n");  
        exit(-1);  
      }  
   pattern[strlen(pattern)-1]='\0';  
      int ch, word = 0;  
      i=0;  
      //put file in to array  
      while ( (ch = fgetc(file)) != EOF ){  
     buffer[i]=ch;  
     i++;  
      }  
   for(i=0;i<sz;i++){  
     printf("%c",buffer[i]);  
   }  
   printf("\n");    
      fclose(file);  
   brute_force(buffer);  
   boyer_moore(buffer);    
     return 0;  
 }  
   
 int boyer_moore(char *buffer){  
   int k=0,j=0,i=0,charcmp=0;  
   if(buffer[i]=='\0'){  
     printf("please choose a file to search\n");  
     return 0;  
   }  
   if(strlen(pattern)==0){  
     printf("please enter a pattern\n");  
     return 0;  
   }  
   if(strlen(buffer)<strlen(pattern)){  
     printf("No matching sub string found");  
     return 0;  
   }  
   printf("Boyer Moore Algorithm (Using extended bad character rule)::\n");  
   i=j=k=strlen(pattern)-1;  
   int l,found=0,not_found=1;  
   while(j<strlen(buffer)){  
     charcmp++;  
     //bad char not found  
     if(pattern[k]==buffer[i]){  
       k--; i--;  
       if(k==-1){  
         printf("\tFound matching substring at %d possition.\n",i+2);  
         j=j+strlen(pattern);  
         i=j;  
         k=strlen(pattern)-1;  
         found=1;  
       }  
     }  
     //bad char found  
     else{  
       int m=0;  
       if(k<1){  
         m=1;  
       }  
       else{  
         for(l=k-1;l>=0;l--){  
           charcmp++;  
           if(buffer[i]==pattern[l]){  
             m++;  
             not_found=0;  
             break;  
           }      
           m++;  
         }  
       }  
       j=j+m;  
       i=j;  
       k=strlen(pattern)-1;  
     }  
   }  
   if(!found){  
     printf("\tNo matching sub string found\n");  
   }  
   printf("\n\tNo of character comparitions = %d\n",charcmp);  
 }   

0 comments:

Post a Comment