-
Notifications
You must be signed in to change notification settings - Fork 0
/
Q1_Bruteforce.c
121 lines (116 loc) · 2.43 KB
/
Q1_Bruteforce.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
//Amit Hampal
//ID: 0964514
//Assignment 3
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.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 swap characters
void swap(char *x, char *y)
{
char temp;
temp = *x;
*x = *y;
*y = temp;
}
//function to check if two strings are anagrams
int isAnagram(char *a, char *b) {
int mapOne[200];
int mapTwo[200];
//length check
if(strlen(a) != strlen(b)) {
return 0;
}
//initialize character map
for(int i = 0; i < 200; i++) {
mapOne[i] = 0;
mapTwo[i] = 0;
}
for(int i = 0; i < strlen(a); i++) {
mapOne[(int)a[i]] += 1;
}
for(int i = 0; i < strlen(b); i++) {
mapTwo[(int)b[i]] += 1;
}
//check if character maps are the same
for(int i = 0; i < 200; i++) {
if(mapOne[i] != mapTwo[i]) {
return 0;
}
}
return 1;
}
//main function
int main(int argc, char **argv) {
char **A = malloc(sizeof(char *) * 30001);
int n;
int j;
char *line = malloc(sizeof(char) * 100);
char *strToFind = malloc(sizeof(char) * 100);
char *tok = malloc(sizeof(char) *10);
char *currStr = malloc(sizeof(char) *100);
FILE *fp;
int searchCount = 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 in contents of file to memory
while(!feof(fp))
{
fgets(line, 100, fp);
line = removeNewLine(line);
tok = strtok(line, " ");
while(tok != NULL) {
A[n] = malloc(sizeof(char) * 100);
strcpy(A[n], tok);
n++;
tok = strtok(NULL, " ");
}
}
//get user input
printf("Anagram to find: ");
fgets(strToFind, 100, stdin);
strToFind = removeNewLine(strToFind);
//bruteforce algorithm
clock_t b = clock();
j = 0;
for(int i = 0; i < n; i++) {
strcpy(currStr, A[i]);
currStr = removeNewLine(currStr);
if(isAnagram(currStr, strToFind) == 1) {
printf("%s is an anagram\n", currStr);
searchCount++;
}
}
clock_t a = clock();
//report results
double time = (double)(a-b) / CLOCKS_PER_SEC;
printf("Number of anagrams: %d\n", searchCount);
printf("Execution time for bruteforce was %lf seconds\n", time);
free(line);
free(strToFind);
fclose(fp);
return 0;
}