-
Notifications
You must be signed in to change notification settings - Fork 1
/
keyComparison.c
110 lines (57 loc) · 1.86 KB
/
keyComparison.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "frequencyAnalysis.h"
int keyComparison(char* deciphertext,keyMap* key,struct keyPhrase* dictionary,int size);
int calculateHits(char* deciphertext,struct keyPhrase* dictionary,int size);
char* convertToUpperCase(char *sPtr);
int keyComparison(char* deciphertext,keyMap* key,struct keyPhrase* dictionary,int size){
//printf("%s\n",deciphertext);
int hits = calculateHits(deciphertext,dictionary,size);
//printf("Hits with current key: %d\n",hits);
return hits;
}
char* convertToUpperCase(char *sPtr)
{
int length = strlen(sPtr);
int i;
for(i = 0;i < length; i++ ) {
sPtr[i] = toupper(sPtr[i]);
}
return sPtr;
}
char* convertToLowerCase(char *sPtr)
{
int length = strlen(sPtr);
int i;
for(i = 0;i < length; i++ ) {
sPtr[i] = tolower(sPtr[i]);
}
return sPtr;
}
int calculateHits(char* deciphertext,struct keyPhrase* dictionary,int size) {
//printf("\n Comparing Deciphered Text to Dictionary...\n\n");
int i;
char buffer[100];
int count = 0;
for(i = 0; i < size; i++) {
if(strstr(convertToUpperCase(deciphertext),convertToUpperCase(dictionary[i].word))){
//printf("%s\n",dictionary[i].word);
if(strlen(dictionary[i].word) == 3){
count+=9;
}
if(strlen(dictionary[i].word) == 4){
count+=16;
}
if(strlen(dictionary[i].word) == 5){
count+=25;
}
if(strlen(dictionary[i].word) >= 6){
count+=36;
}
count++;
}
}
return count;
}