-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.c
79 lines (64 loc) · 1.25 KB
/
functions.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
/* Implementation of the functions in project two */
#include "functions.h"
char * removeDuplicates(char word[])
{
int i, j, k;
int size = strlen(word);
for(i = 0; i < size; i++)
{
for(j = i + 1; j < size; j++)
{
if(word[j] == word[i])
{
for(k = j; k < size; k++)
{
word[k] = word[k + 1];
}
size--;
j--;
}
}
}
return word;
}
void initializeEncryptArray(char key[], char encrypt[])
{
char* alphabet = "ZYXWVUTSRQPONMLKJIHGFEDCBA";
char* temp = malloc(strlen(key) + 27);
int i, j, count;
strcpy(temp, key);
strcat(temp, alphabet);
removeDuplicates(temp);
for(i = 0; i < MAXNUM; i++)
encrypt[i] = temp[i];
encrypt[MAXNUM] = '\0';
free(temp);
}
void initializeDecryptArray(char encrypt[], char decrypt[])
{
int i;
for(i = 0; i < MAXNUM; i++)
decrypt[encrypt[i] - 'A'] = i + 65;
decrypt[MAXNUM] = '\0';
}
void processInput(char* inf, char* outf, char substitute[])
{
FILE *fpin, *fpout;
char ch;
fpin = fopen(inf, "r");
fpout = fopen(outf, "w");
if(fpin == NULL)
{
printf("File could not be opened\n");
exit(1);
}
while(fscanf(fpin, "%c", &ch) != EOF)
{
if(isalpha(ch))
fprintf(fpout, "%c", substitute[ch - 'A']);
else
fprintf(fpout, "%c", ch);
}
fclose(fpin);
fclose(fpout);
}