-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmostfrequent.h
More file actions
68 lines (56 loc) · 1005 Bytes
/
Copy pathmostfrequent.h
File metadata and controls
68 lines (56 loc) · 1005 Bytes
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
#ifndef MOSTOCCURANCE_H
#define MOSTOCCURANCE_H
#include <readline/readline.h>
#include <stdio.h>
int count[1000];
char strings[1000][1000];
int lastIndex = 0;
int search(char *str)
{
int i;
for(i = 0; i < lastIndex; i++){
if(strcmp(str, strings[i]) == 0)
return 1;
}
return 0;
}
void update(char *str)
{
int i;
for(i = 0; i < lastIndex; i++){
if(strcmp(str, strings[i]) == 0){
count[i]++;
return;
}
}
}
void mostFrequent(char *fileName)
{
FILE *file;
file = fopen(fileName, "r");
char buf[1000];
int maxIndex = 0, max = 0;
int i;
while(fgets(buf, 1000, file) != NULL){
//buf = readline("");
//if(strcmp(buf, "e") == 0)
// break;
if(search(buf)){
//printf("1\n");
update(buf);
}
else{
//strings[lastIndex++] = buf;
strcpy(strings[lastIndex++], buf);
}
}
for(i = 0; i < lastIndex; i++){
if(count[i] > max){
max = count[i];
maxIndex = i;
}
}
printf("\nmost frequent: %s\n", strings[maxIndex]);
fclose(file);
}
#endif