forked from jye-hollier/proj1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtaskFive.c
152 lines (135 loc) · 5.6 KB
/
taskFive.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
#include <stdio.h>
#include <string.h>
void taskFive(void) {
char originalText[] = "TVU TVAOTH: AOL KHAH IYVBNOA AV BZ IF AOL IVAOHU ZWPLZ WPUWVPUAZ AOL LEHJA SVJHAPVU VM AOL LTWLYVY'Z ULD IHAASL ZAHAPVU. DL HSZV RUVD AOHA AOL DLHWVU ZFZALTZ VM AOPZ KLHAO ZAHY HYL UVA FLA VWLYHAPVUHS. DPAO AOL PTWLYPHS MSLLA ZWYLHK AOYVBNOVBA AOL NHSHEF PU H CHPU LMMVYA AV LUNHNL BZ, PA PZ YLSHAPCLSF BUWYVALJALK. IBA TVZA PTWVYAHUA VM HSS, DL'CL SLHYULK AOHA AOL LTWLYVY OPTZLSM PZ WLYZVUHSSF VCLYZLLPUN AOL MPUHS ZAHNLZ VM AOL JVUZAYBJAPVU VM AOPZ KLHAO ZAHY. THUF IVAOHUZ KPLK AV IYPUN BZ AOPZ PUMVYTHAPVU.";
//char originalText[1000];
//printf("Enter text to be decrypted\n\n");
//scanf("%[^\n]%*c", originalText);
int stringLength = strlen(originalText);
int letterCount[26];
char decryptedText[stringLength];
int firstCommon = 0;
int firstCommonAmount = 0;
int secondCommon = 0;
int secondCommonAmount = 0;
int thirdCommon = 0;
int thirdCommonAmount = 0;
int i;
int key;
char checker;
//static int attempt = 0;
//attempt++;
for(i = 0; i < 26; i++) {
letterCount[i] = 0;
}
for(i = 0; i < stringLength; i++) {
switch(originalText[i]) {
case 'A': letterCount[0]++;
break;
case 'B': letterCount[1]++;
break;
case 'C': letterCount[2]++;
break;
case 'D': letterCount[3]++;
break;
case 'E': letterCount[4]++;
break;
case 'F': letterCount[5]++;
break;
case 'G': letterCount[6]++;
break;
case 'H': letterCount[7]++;
break;
case 'I': letterCount[8]++;
break;
case 'J': letterCount[9]++;
break;
case 'K': letterCount[10]++;
break;
case 'L': letterCount[11]++;
break;
case 'M': letterCount[12]++;
break;
case 'N': letterCount[13]++;
break;
case 'O': letterCount[14]++;
break;
case 'P': letterCount[15]++;
break;
case 'Q': letterCount[16]++;
break;
case 'R': letterCount[17]++;
break;
case 'S': letterCount[18]++;
break;
case 'T': letterCount[19]++;
break;
case 'U': letterCount[20]++;
break;
case 'V': letterCount[21]++;
break;
case 'W': letterCount[22]++;
break;
case 'X': letterCount[23]++;
break;
case 'Y': letterCount[24]++;
break;
case 'Z': letterCount[25]++;
break;
}
}
for(i = 0; i < 26; i++) {
if(letterCount[i] >= firstCommonAmount) {
thirdCommon = secondCommon;
secondCommon = firstCommon;
firstCommon = i;
thirdCommonAmount = secondCommonAmount;
secondCommonAmount = firstCommonAmount;
firstCommonAmount = letterCount[i];
}
else if (letterCount[i] < firstCommonAmount && letterCount[i] >= secondCommonAmount) {
thirdCommon = secondCommon;
secondCommon = i;
thirdCommonAmount = secondCommonAmount;
secondCommonAmount = letterCount[i];
}
else if(letterCount[i] < secondCommonAmount && letterCount[i] >= thirdCommonAmount) {
thirdCommon = i;
thirdCommonAmount = letterCount[i];
}
}
key = (firstCommon + 22) %26;
for(i = 0; i < stringLength + 1; i++) {
if (originalText[i] > 64 && originalText[i] < 91) {
decryptedText[i] = (((originalText[i] - 39) + key) %26 ) + 65;
}
else
decryptedText[i] = originalText[i];
}
printf("Decrypted text:\n%s\n\nIf this text does not make sense enter 'r' to retry\nIf correct enter any other to exit\n", decryptedText);
scanf("%c", &checker);
if (checker == 'r' || checker == 'R') {
key = (secondCommon + 22) %26;
for(i = 0; i < stringLength + 1; i++) {
if (originalText[i] > 64 && originalText[i] < 91) {
decryptedText[i] = (((originalText[i] - 39) - key) %26 ) + 65;
}
else
decryptedText[i] = originalText[i];
}
printf("Decrypted text:\n%s\n\nIf text does not make sense enter 'r' to retry again\nIf correct enter any other key to exit\n", decryptedText);
scanf("%c", &checker);
if (checker == 'r' || checker == 'R') {
key = (thirdCommon + 22) %26;
for(i = 0; i < stringLength + 1; i++) {
if (originalText[i] > 64 && originalText[i] < 91) {
decryptedText[i] = (((originalText[i] - 39) + key) %26 ) + 65;
}
else
decryptedText[i] = originalText[i];
}
printf("Decrypted text:\n%s\n\nIf text does not make sense the text unfortunately cannot be decrypted\n", decryptedText);
scanf("%c", &checker);
}
}
}