-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhw4.c
57 lines (52 loc) · 1.19 KB
/
hw4.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
#include <stdio.h>
char Decrypt(char letter)
{
FILE *codekey = fopen("substitution.txt", "r");
char dLetter = '1';
int i = -1;
if (codekey == NULL)
{
printf("substitution.txt couldn't be opened.\n");
return -1;
}
while (letter != dLetter)
{
fscanf(codekey, "%c", &dLetter);
i++;
}
fclose(codekey);
return 'a' + i;
}
int main()
{
FILE *encrypted = fopen("encrypted.txt", "r");
FILE *decrypted = fopen("decrypted.txt", "w");
char letter;
if (encrypted == NULL)
{
printf("encrypted.txt couldn't be opened.\n");
return -1;
}
while (!feof(encrypted))
{
fscanf(encrypted, "%c", &letter);
if (letter >= 'A' && letter <= 'Z')
{
letter = letter - 'A' + 'a';
letter = Decrypt(letter);
letter = letter - 'a' + 'A';
fprintf(decrypted, "%c", letter);
}
else if (letter >= 'a' && letter <= 'z')
{
fprintf(decrypted, "%c", Decrypt(letter));
}
else
{
fprintf(decrypted, "%c", letter);
}
}
fclose(encrypted);
fclose(decrypted);
return 0;
}