#include <stdio.h>
#include <conio.h>
#include <ctype.h>
void entryData();
void decodeFile();
int indexPosition(char);
char getEncodeChar(int);
char getDecodeChar(int);
void getWriteFileName(char *str);
void getReadFileName(char *str);
char alpha[26] = "abcdefghijklmnopqrstuvwxyz";
char filename[14];
char readname[14];
int option;
FILE *fp;
void main()
{
clrscr();
printf("\nSelect any of the option from below.");
printf("\n1. Create Encode File");
printf("\n2. Reade Encode File\n");
scanf("%d", &option);
fflush(NULL);
if( option == 1)
getWriteFileName(filename);
elseif( option == 2)
{ getReadFileName(readname);
decodeFile();
}
getch();
}
void entryData()
{
char chr;
char prevchr;
int index=-1;
prevchr = chr;
printf("\n Just type '::' to terminate typing. Start typing\n");
fflush(NULL);
while(1)
{
scanf("%c", &chr);
chr = tolower(chr);
if( chr == ':')
if( prevchr == chr)
break;
if( isalpha(chr) )
{
index = indexPosition(chr);
chr = getEncodeChar(index);
}
putc(chr, fp);
prevchr = chr;
}
}
void getWriteFileName(char *str)
{
printf("Enter Output File name maxium 14 character : ");
scanf("%s", str);
if( ( fp = fopen(str, "wt") ) == NULL )
{ fclose(fp);
printf("\nInvalid file name entered.");
}
else
entryData();
}
void getReadFileName(char *str)
{
printf("Enter Input File name maxium 14 character : ");
scanf("%s", str);
if( ( fp = fopen(str, "r+t") ) == NULL )
{ fclose(fp);
printf("\nInvalid file name entered.");
}
}
void decodeFile()
{ char rchar;
int index=-1;
while( feof(fp) == 0 )
{
rchar = getc(fp);
if( isalpha(rchar) )
{
index = indexPosition(rchar);
rchar = getDecodeChar(index);
}
printf("%c", rchar);
}
}
char getEncodeChar(int index)
{
int tmp=0;
tmp = index + 5;
if ( tmp > 25)
tmp = tmp - 26;
return alpha[tmp];
}
char getDecodeChar(int index)
{
int tmp=0;
tmp = index - 5;
if ( tmp < 0)
tmp = 26 + tmp;
return alpha[tmp];
}
int indexPosition(char chr)
{
int count=0;
for( ; count < 26; count++)
{
if ( alpha[count] == chr)
break;
}
return count;
}
/*******************Input*******************************
Select any of the option from below.
1. Create Encode File
2. Reade Encode File
1
Enter Output File name maxium 14 character : output.txt
contain of output.txt is
ymnx nx jcfruqj tk knqj jshtinsl.
:
2 Read Encoded File
******************* OutPut*******************************
Enter Input File name maxium 14 character : output.txt
this is example of file encoding.
:
*/