#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
void main()
{
void Inputsource(char *sourcefile);
void Outputdoc(char *docfile);
void encode(char *sourcefile,char *codefile);
void decode(char *codefile,char *docfile);
char *sourcefile="source_doc.dat";
char *codefile="code.dat";
char *docfile="doc.dat";
Inputsource(sourcefile);
encode(sourcefile,codefile);
decode(codefile,docfile);
Outputdoc(sourcefile);
Outputdoc(codefile);
Outputdoc(docfile);
}
void Inputsource(char *sourcefile)
{
FILE *fpw_source;
char ch;
if((fpw_source=fopen(sourcefile,"w"))==NULL)
{
printf("Open source file Failure!!/n");
exit(0);
}
printf("In put your source doc ,and '!' for end :/n");
while((ch=getchar())!='!')
{
fputc(ch,fpw_source);
}
fclose(fpw_source);
}
void Outputdoc(char *docfile)
{
FILE *fpr_doc;
char ch;
if((fpr_doc=fopen(docfile,"r"))==NULL)
{
printf("Open docfile file Failure!!/n");
exit(0);
}
printf("The text of %s is :/n",docfile);
while((ch=fgetc(fpr_doc))!=EOF)
{
putchar(ch);
}
fclose(fpr_doc);
}
char encode_func(char ch)
{
short int chint=ch;
short int chintz='z';
short int chintZ='Z';
if(isalpha(chint))
{
if( ch>='a' )
chint=(chint-'a'+5)%26+'a';
else
chint=(chint-'A'+5)%26+'A';
}
return char(chint);
}
char decode_func(char ch)
{
short int chint =ch;
short int chintz='a';
short int chintZ='A';
if(isalpha(chint))
{
if( ch<='Z' )
chint=(chint-'A'+21)%26+'A';
else
chint=(chint-'a'+21)%26+'a';
}
return char(chint);
}
void encode(char *sourcefile,char *codefile)
{
FILE *fpr_source,*fpw_code;
char ch;
if((fpr_source=fopen(sourcefile,"r"))==NULL)
{
printf("Open source file Failure!!/n");
exit(0);
}
if((fpw_code=fopen(codefile,"w"))==NULL)
{
printf("Open code file Failure!!/n");
exit(0);
}
while((ch=fgetc(fpr_source))!=EOF)
{
ch=encode_func(ch);
fputc(ch,fpw_code);
}
fclose(fpr_source);
fclose(fpw_code);
}
void decode(char *codefile,char *docfile)
{
FILE *fpw_doc,*fpr_code;
char ch;
if((fpw_doc=fopen(docfile,"w"))==NULL)
{
printf("Open doc file Failure!!/n");
exit(0);
}
if((fpr_code=fopen(codefile,"r"))==NULL)
{
printf("Open code file Failure!!/n");
exit(0);
}
while((ch=fgetc(fpr_code))!=EOF)
{
ch=decode_func(ch);
fputc(ch,fpw_doc);
}
fclose(fpw_doc);
fclose(fpr_code);
}