DukeRick81
06-17-2005, 01:30 PM
I need to use the boyer-moore on a text file (.txt) and the thing is just not working at all. Every single example for this algorithm is based on a string and so I figured out it could work if I called the bmsearch function per each line. This is the source code:
#include <alloc.h>
#include <string.h>
# include <iostream.h>
#include <stdio.h>
#include <conio.h>
int bmsearch(char *text, char *pat, int *no, int *pos)
{
int i, table[256];
int len=strlen(pat);
*no=0;
int compcount=0;
for(i=0; i<256; i++) table[i]=len;
for(i=0; i<len; i++) table[pat[i]]=len-(i+1);
int ptct=len-1;
while (ptct<strlen(text))
{
int count=0;
while (count<len)
{
if (text[ptct-count]!=pat[len-1-count]) {compcount++;break;}
else count++;
}
if (count==len)
{
(*no)++;
*(pos+(*no)-1)=(ptct-count+1);
ptct+=len;
}
else
{
ptct+=(table[text[ptct-count]]-count);
}
}
return compcount;
}
int main(void)
{
FILE *stream;
char file[120], ch, cadena[200], CadBus[100];
double indice;
int no, count, pos[200], i;
indice=0;
clrscr();
printf("\nString?:"); cin>>CadBus;
stream = fopen("rvtest.txt", "r");
fseek(stream, 0, SEEK_SET);
do{
ch = fgetc(stream);
cadena[indice]=ch;
if (cadena[indice]!='\n' /*&& cadena[indice]!='\0'*/)
{
indice++;
}
else
{
// might use count somewhere below..
count=bmsearch(cadena, CadBus, &no, pos);
cout<<"\n"<<no<<" Match found.";
indice=0; // so the array is empty now
//it doesnt work
}
}while (ch != EOF);
fclose(stream);
getch();
return 0;
}
Here is the rvtest2.txt file:
juan perez
ricardo ignacio varas santana
gerardo andres varas santana
tanya marcella karrer
duke nukem
Octopus
Duke forever
URL
Email Image Code
hello world
I love C++
Garland
Texas
end of file
----------------
Well I dont know where's the bug even when I've tried to find it. Anyone can help me? Thanks in advance.
#include <alloc.h>
#include <string.h>
# include <iostream.h>
#include <stdio.h>
#include <conio.h>
int bmsearch(char *text, char *pat, int *no, int *pos)
{
int i, table[256];
int len=strlen(pat);
*no=0;
int compcount=0;
for(i=0; i<256; i++) table[i]=len;
for(i=0; i<len; i++) table[pat[i]]=len-(i+1);
int ptct=len-1;
while (ptct<strlen(text))
{
int count=0;
while (count<len)
{
if (text[ptct-count]!=pat[len-1-count]) {compcount++;break;}
else count++;
}
if (count==len)
{
(*no)++;
*(pos+(*no)-1)=(ptct-count+1);
ptct+=len;
}
else
{
ptct+=(table[text[ptct-count]]-count);
}
}
return compcount;
}
int main(void)
{
FILE *stream;
char file[120], ch, cadena[200], CadBus[100];
double indice;
int no, count, pos[200], i;
indice=0;
clrscr();
printf("\nString?:"); cin>>CadBus;
stream = fopen("rvtest.txt", "r");
fseek(stream, 0, SEEK_SET);
do{
ch = fgetc(stream);
cadena[indice]=ch;
if (cadena[indice]!='\n' /*&& cadena[indice]!='\0'*/)
{
indice++;
}
else
{
// might use count somewhere below..
count=bmsearch(cadena, CadBus, &no, pos);
cout<<"\n"<<no<<" Match found.";
indice=0; // so the array is empty now
//it doesnt work
}
}while (ch != EOF);
fclose(stream);
getch();
return 0;
}
Here is the rvtest2.txt file:
juan perez
ricardo ignacio varas santana
gerardo andres varas santana
tanya marcella karrer
duke nukem
Octopus
Duke forever
URL
Email Image Code
hello world
I love C++
Garland
Texas
end of file
----------------
Well I dont know where's the bug even when I've tried to find it. Anyone can help me? Thanks in advance.