/  C Programs   /  File program to read and encrypts character

File program to read and encrypts character

program to read a character file and encrypts it by replacing each alphabet by its next alphabet cyclically i.e., z is replaced by a. Non- alphabets­­ in the file are retained as they are. To write the encrypted text into another file.

 

#include<stdio.h>
#include<stdlib.h>
main()
{
FILE *fp1,*fp2;
char fname1[20],fname2[20];
char ch1,ch2;
printf("Enter the source file name ");
scanf("%s",fname1);
fp1=fopen(fname1,"r");
if(fp1==NULL)
printf("%s is not available",fname1);
else
{
printf("Enter the new file name ");
scanf("%s",fname2);
fp2=fopen(fname2,"w");
while((ch1=fgetc(fp1))!=EOF)
{
printf("%c",ch1);
if((ch1>=65 && ch1<=89) || (ch1>=97 && ch1<=121))
ch2=ch1+1;
else if(ch1==90 || ch1==122)
ch2=ch1-25;
else
ch2=ch1;
fputc(ch2,fp2);
}
printf("File copied into %s succesfull",fname2);
fclose(fp1);
fclose(fp2);
}
}

 

Output:

 

File program to read and encrypts character

 

Input text apple.txt

 

File program to read and encrypts character

 

Output text orange.txt

 

File program to read and encrypts character

 

Leave a comment