-
Notifications
You must be signed in to change notification settings - Fork 0
/
Q2_Horspool.c
101 lines (96 loc) · 2.24 KB
/
Q2_Horspool.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
//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 create bad match table
void shiftTable(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;
}
}
//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 *table = malloc(sizeof(int) * 1000);
int patternShift = 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;
//read file into memory
printf("Loading data...\n");
while(!feof(fp))
{
fgets(line, 100, fp);
line = removeNewLine(line);
strcat(A, line);
n++;
}
//get user input and create shift table
printf("String to find: ");
fgets(strToFind, 100, stdin);
strToFind = removeNewLine(strToFind);
shiftTable(strToFind, table);
int i = strlen(strToFind) - 1;
int k;
clock_t b = clock();
int patternLen;
patternLen = strlen(A);
while(i <= (patternLen - 1)) {
k = 0;
patternShift++;
while (k <= (strlen(strToFind) - 1) && strToFind[strlen(strToFind) - 1 - k] == A[i - k]) {
k++;
}
//shift by entire length of pattern if found match
if(k == strlen(strToFind)) {
searchCount++;
i = i + strlen(strToFind);
}
//otherwise shift in accordance to bad match table
else {
i = i + table[abs((int)A[i] - 32)];
}
}
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 horspool was %lf seconds\n", time);
fclose(fp);
return 0;
}