-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQ2_Boyer_Moore.c
171 lines (162 loc) · 3.92 KB
/
Q2_Boyer_Moore.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
//Amit Hampal
//ID: 0964514
//Assignment 3
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <ctype.h>
//function strips input of new line and replaces with null
char *removeNewLine(char *file) {
for(int i = 0; i < strlen(file); i++) {
if(*(file + i) == '\n') {
*(file + i) = '\0';
}
}
return file;
}
//function to return max of two integers
int max(int a, int b) {
if( a > b) {
return a;
}
return b;
}
//function to create bad match table
void badTable(char *strToFind, int *table) {
for(int p = 0; p < 1000; p++) {
table[p] = strlen(strToFind);
}
for(int i = 0; i < (strlen(strToFind) - 1); i++) {
table[(int)(strToFind[i] - 32)] = strlen(strToFind) - i - 1;
}
}
//Standard C library function strstr, but modified to return right most value
char *rstrstr(char *s1, char *s2)
{
size_t s1len = strlen(s1);
size_t s2len = strlen(s2);
char *s;
if (s2len > s1len) {
return NULL;
}
for (s = s1 + s1len - s2len; s >= s1; s--) {
if (strncmp(s, s2, s2len) == 0) {
return s;
}
}
return NULL;
}
//function to create good suffix table
void goodTable(char *strToFind, int *table) {
int len = strlen(strToFind);
char *suff = malloc(sizeof(char) * len);
char *pref = malloc(sizeof(char) * len);
char *potentialPref = malloc(sizeof(char) * len);
char *index;
char *indexTwo;
for(int p = 0; p < 1000; p++) {
table[p] = 1;
}
for(int p = len - 1; p > 0; p--) {
//stores suffix and prefix into strings
strcpy(suff, strToFind + p);
strncpy(pref, strToFind, p);
pref[p] = '\0';
index = rstrstr(pref, suff);
//finds if suffix exists in prefix
if(index != NULL) {
table[strlen(suff)] = strlen(strToFind) - strlen(index);
}
//case for if it doesn't
else {
//attempts to find smaller suffix before setting table value to string length
for(int l = 1; l < len; l++) {
strncpy(potentialPref, strToFind, l);
pref[l] = '\0';
indexTwo = strstr(suff, pref);
if(indexTwo != NULL) {
table[strlen(suff)] = strlen(strToFind) - strlen(suff) - strlen(pref);
}
else {
table[strlen(suff)] = strlen(strToFind);
}
}
}
}
}
//main function
int main(int argc, char **argv) {
char A[4405000];
int n;
char *line = malloc(sizeof(char) * 100);
FILE *fp;
char *strToFind = malloc(sizeof(char) * 100);
int searchCount = 0;
int *bTable = malloc(sizeof(int) * 1000);
int *gTable = malloc(sizeof(int) * 1000);
int patternShift = 0;
int patternLen = 0;
//open file for reading
printf("File is: %s\n", argv[argc - 1]);
if(argc > 1)
{
fp = fopen( argv[argc - 1], "r" );
}
else
{
return 0;
}
if(fp == NULL)
{
printf("Cannot find file. Ensure your syntax is correct. Exiting program.\n");
return 0;
}
n = 0;
//load data into memory
printf("Loading data...\n");
while(!feof(fp))
{
fgets(line, 100, fp);
line = removeNewLine(line);
strcat(A, line);
patternLen += strlen(line);
n++;
}
A[n] = '\0';
//get user input
printf("String to find: ");
fgets(strToFind, 100, stdin);
strToFind = removeNewLine(strToFind);
//create tables based off user input
badTable(strToFind, bTable);
goodTable(strToFind, gTable);
int i = strlen(strToFind) - 1;
int k;
clock_t b = clock();
//similar algorithm to horspool, but takes max of two tables instead
while(i <= (patternLen - 1)) {
k = 0;
patternShift++;
while (k <= (strlen(strToFind) - 1) && strToFind[strlen(strToFind) - 1 - k] == A[i - k]) {
k++;
}
//if match found, shift by entire length of pattern
if(k == strlen(strToFind)) {
searchCount++;
i = i + strlen(strToFind);
}
//otherwise shift in accordance with tables
else {
i = i + max(bTable[abs((int)A[i] - 32)], gTable[k]);
}
}
clock_t a = clock();
//report results
double time = (double)(a-b) / CLOCKS_PER_SEC;
printf("Number of Occurances is: %d\n", searchCount);
printf("Number of patternshifts is: %d\n", patternShift);
printf("Execution time for boyer-moore was %lf seconds\n", time);
fclose(fp);
return 0;
}