//Caesar cipher
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
int readfile(char myarray[])
{
FILE *fp;
int i = 0;
char c;
fp=fopen("abc.txt","r");
while(! feof(fp))
{
c=fgetc(fp);
myarray[i]=c;
i++;
}
fclose(fp);
cout<<myarray;
getch();
return i;
}
void main()
{
FILE *ft;
char myarray[50];
char cipher[50];
char key;
int pos,n;
clrscr();
cout<<"The text from disk is :"<<endl;
n=readfile(myarray);
cout<<"\nEnter the encryption key :";
cin>>key;
pos =tolower(key)-96;
for(int i =0;i<n;i++)
{
if(myarray[i]>='a' && myarray[i]<='z')
{
cipher[i]=myarray[i]+pos;
if(cipher[i]>122 )
cipher[i]=cipher[i]-26;
}
else if(myarray[i]>='A' && myarray[i]<='Z')
{
cipher[i]=myarray[i]+pos;
if(cipher[i]>90)
cipher[i]=cipher[i]-26;
}
else
cipher[i]=myarray[i];
}
cout<<cipher;
ft=fopen("xyz.txt","w");
for(i=0;i<n;i++)
{
putc(cipher[i],ft);
}
getch();
}
No comments:
Post a Comment