Search This Blog

Monday 18 January 2016

Cipher text example with file handling

 Simple Cipher text with file Handling
Input : hello karan gajjar
key : 3
Output : khoor ndudq jdmmdu

As we know encryption and decryption. I have kept input in encode.txt file and aftyer entering key value the decrypted text will be stored in decode.txt file!

CODE:

 #include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
    int i,key,k;
    char str[100],str2[100];
    FILE *fp,*fq;
    fp=fopen("encode.txt","r");
    fgets(str,sizeof(str),fp);
    puts(str);
    fq=fopen("decode.txt","a");
    clrscr();
    printf("Enter Key:");
    scanf("%d",&key);
    for(i=0;i<strlen(str);i++)
    {
        if(str[i]>=97 && str[i]<120)
        {
            str[i]=str[i]+key;
            str2[i]=str[i];
        }
        else if(str[i]==32)
        {
            str2[i]=str[i];
        }
        else
        {
            str[i]=str[i]-26+key;
            str2[i]=str[i];
        }
    }
    str2[i]='\0';
    puts(str2);
    fprintf(fq,"\n");
    fprintf(fq,str2);
    fclose(fq);
    fclose(fp);
    getch();
}


NOTE: You can add more elseif condition for "," , "!" , "?" this type of ascii value i have added for "space".
              and yes in this example you can't add encoded message with every new line!

No comments:

Post a Comment