Question

So as of right now, my substitution cipher program seems to work pretty nicely; however, i am still having the problem.

I must print the substitution table such as below, the table must be printed into the output file before the encryption :

EXAMPLE:

a m

b n

etc...

z q

A P

B O

etc...

Z X

0 5

1 4

etc...

9 0

/ENCRYPTED MESSAGE HERE/

CODE:

#include <stdio.h>
char input[]={'\n',' ','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','0','1','2','3','4','5','6','7','8','9'};
char encrypted[]={'\n',' ','m','n','b','v','c','x','z','a','s','d','f','g','h','j','k','l','p','o','i','u','y','t','r','e','w','q','P','O','I','U','Y','T','R','E','W','Q','A','S','D','F','G','H','J','K','L','M','N','B','V','C','X','Z','5','4','3','2','1','6','7','8','9','0'};

int c;
//int j=0;

int main(){
    FILE *file1=fopen("projectinput.txt", "r");
    FILE *file2=fopen("projectoutput.txt", "w");
    //fprintf(file2,"%c %c\n",input[j++],encrypted[j++]);
    while((c=fgetc(file1))!=EOF){
        int i=0;
        while(input[i]!=c){
            i++;
        }
        fputc(encrypted[i], file2);

}
return 0;
}
Was it helpful?

Solution

Use fprintf in a loop:

for(int i = 2; i < sizeof input; i++)
    fprintf(file "%c %c\n", input[i], encrypted[i]);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top