Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

cipherText: cipher.txt: Template.c: Hellol I\'m working on a C programming homew

ID: 3571250 • Letter: C

Question

cipherText:

cipher.txt:

Template.c:

Hellol I'm working on a C programming homework and I am struggling. l would really appreciate your expertise. Bellow there is a cipherText.txt file. If you open it, it will make no sense. The cipher is easy, but takes too long to apply by hand. Let's make a program that reads in a cipher input and produces a translated output The program's overall structure is this: Open two files, one for input "r" and one for output "w" Read a line from the input file into an input string Go through character from the input string and put its cipher substitution equivalent into an output string Output the string to the output file When finished, the output file should be a readable famous paper I have attached a template that explains thoroughly what to do What you need to do: 1) Download cipher Text.txt to your project directory (or a location you can easily access) 2) Download cipher.txt to your project directory as well 3) Download "Tem ate.c 4) Go through the template and fill in all the TODO: comments Alternatively you can use the template as guidelines and attempt to write your own solution to this problem. Some of you may find this easier YOUR HELP IS MUCH APPRECIATED!

Explanation / Answer

NOTE : I am using gets() to read the input and output file names because fgets() is not working properly in my system. It may give a minor depreciation warning at compile time but the program will execute fine. You can also un-comment fgets() statements and run the program. [If you want to use gets_s, fopen_s, you can just replace them as you have done earlier in the template.]

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define CIPHER_SIZE 52

typedef struct
{
        char originalCharacter;
        char cipherCharacter;
} Cipher;


void ReadCipherFile(Cipher *cipherArray)
{
        FILE *fp;
        int i = 0;
        fp = fopen("cipher.txt", "r");
        while (!feof(fp))
        {
                //Read the original character
                cipherArray[i].originalCharacter = fgetc(fp);

                //Read the cipher character
                cipherArray[i].cipherCharacter = fgetc(fp);

                //Read the newline character
                fgetc(fp);

                i++;
        }
}


//Function Input: character to subsitute and cipher array
//Function output: Subsituted character if part of cipher array
//Description: Given a character and the cipher array, return the substitution if it exists
char CipherSub(char in, const Cipher *cipherArray)
{
        int i;
      
        // Do a linear search from 0 to CIPHER_SIZE. If in == originalCharacter then return cipherCharacter
        for (i = 0; i<CIPHER_SIZE; i++)
        {
            if (in == cipherArray[i].originalCharacter)
            {
                return cipherArray[i].cipherCharacter;
            }         
        }
      
        //If not found, return -1
        return -1;
}

//Function Input: String to cipher and cipher array
//Function Output: Ciphered string (passed by reference)
//Description: Given a string input, create an output string that is all substituted character
//For example: A string input of "abc123." would have an output string of "zyx123."
void ProcessCipher(char *input, char *output, const Cipher *cipherArray)
{
        int i=0;
        for (i = 0; i < strlen(input); i++)
        {
            int value = CipherSub(input[i], cipherArray);
            if (value == -1)
            {
                output[i] = input[i];
            }          
            else {
                output[i] = (char) value;
            }
        }  
      
        //Output isn't really a string yet, need to end it with ''
        output[i] = '';
}

//Function Input: File pointers for input, output and cipher array
//Function Output: None
//Description: Go through the input file and process string by string. Take every
//de-ciphered string and output it to the output file
void ProcessFiles(FILE *fIn, FILE *fOut, const Cipher *cipherArray)
{
        char bufferIn[10000];
        char bufferOut[10000];

        while (fgets(bufferIn, 10000, fIn))
        {
            ProcessCipher(bufferIn, bufferOut, cipherArray);
            fputs(bufferOut, fOut);
        }
}

//The main function
void main()
{
        Cipher cipherArray[CIPHER_SIZE];
        FILE *fIn=0;
        FILE *fOut=0;
        char fileNameIn[1000];
        char fileNameOut[1000];

        //Read our cipher file
        ReadCipherFile(cipherArray);

        //Figure out the name of input and output files
       printf("Enter the file to input: ");
        gets(fileNameIn);
       //fgets(fileNameIn, 1000, stdin);
        printf("Enter the file to output: ");
        gets(fileNameOut);
        //fgets(fileNameOut, 1000, stdin);

        // Open input and output files using fopen_s
        fIn = fopen(fileNameIn, "r" );
        fOut = fopen(fileNameOut, "w+");

       //Call ProcessFiles
        ProcessFiles(fIn, fOut, cipherArray);

        //Close the files for proper cleanup
        fclose(fIn);
        fclose(fOut);
}