include stdio include stdlib include ctype int encode int int int enco

 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
#include<stdio.h>
#include<stdlib.h>
#include <ctype.h>
int encode (int, int);
int encode(int ch, int key) {
if (islower(ch)) {
ch = (ch-'a' + key) % 26 + 'a';
ch += (ch < 'a') ? 26 : 0;
}
else if (isupper(ch)) {
ch = (ch-'A' + key) % 26 + 'A';
ch += (ch < 'A') ? 26 : 0;
}
return ch;
}
int main (void)
{
FILE *file_in;
FILE *file_out;
char ch;
char text[300];
int key;
printf("Enter the name of the file you wish to see including complete pathway and file\n extension:\n");
gets(text);
file_in = fopen(text,"r");
if( file_in == NULL )
{
perror("Error while opening the file.\n");
exit(EXIT_FAILURE);
}
printf("\n The contents of the file are : \n");
while( ( ch = fgetc(file_in) ) != EOF )
{
printf("%c",ch);
}
printf("\n \n Enter the name of the file you wish to send the encryption\n to including complete pathway and file\n extension:\n");
gets(text);
file_out = fopen(text,"w");
printf("\n Enter the alphabetic offset key you would like to use:");
scanf("%d", &key);
while( ( ch = fgetc(file_in) ) != EOF )
{
encode(ch, key);
fprintf(file_out, "%c", ch);
}
printf("file has been encoded");
fclose(file_out);
fclose(file_in);
return 0;