Question

In: Computer Science

In this assignment you will write a program that encrypts a text file.  You will use the...

In this assignment you will write a program that encrypts a text file.  You will use the following encryption scheme.

  1. Ask the user for the name of the original file.
  2. Ask the user for the name of the output file.
  3. Ask the user for the encryption key, n.
  4. Read n2 characters from the file into the n rows and n columns of a 2-dimensional array.
  5. Transpose the array.  (Exchange the rows and columns.)
  6. Write the characters from the array to an output file.
  7. Repeat steps 4 – 6 until the entire input file has been read.

This same program can be used to decrypt a file that has been encrypted in this way.

Your program should have functions for performing each of the following tasks.

  1. Reading the characters from the file into the array.  This function should be passed the file stream, the array and the key.
  2. Transposing the array.  This function should be passed the array and the key.  (Make sure you include this function as this is an exercise in parameter passing.)
  3. Writing the characters from the array to the file.  This function should be passed the file stream, the array and the key.

Your main function should get the filenames, open the files, get the key, dynamically allocate the memory for the array, and close the files.

Your program should also:

Give detailed directions and warnings to the user.

  • Be readable with appropriate documentation and formatting.  Be sure to include carefully written pre and post conditions for each of your functions.

Solutions

Expert Solution

Code is as following:

#include <iostream>
#include <fstream>
#include <bits/stdc++.h>

using namespace std;
//transpose the array
void transpose(char *A, char *B, int n){
  int i, j; 
    for (i = 0; i < n; i++) 
        for (j = 0; j < n; j++) 
            *((B+i*n)+j) = *((A+j*n)+i); 
}

//read the characters to the array
void readCharsToArray(ifstream &fin,char *arr,int n){
  char ch;
  for(int i=0; i<n; i++){
        for(int j=0; j<n; j++){
          if(fin >> noskipws >> ch)
            *(arr+(i*n)+j) = ch;
          else
            break;
        }
  }
}

int main() {
  int n = 0; string inFile, outFile;
  cout<<"Enter input file name: ";
  cin>>inFile;
  cout<<"Enter output file name: ";
  cin>>outFile;
  while(n<1){
    cout<<"Enter key('n'):"<<endl;
    cin>>n;
    if(n<1)
      cout<<"Enter valid key!!"<<endl;
  }

  //construct n-by-n matrix
  char arr[n][n];
  char temp[n][n];
  memset(arr,'$',sizeof(arr));


  //open input file
  ifstream fin(inFile);

  ofstream fout;
  //first open output file to format all content, if it already exists
  fout.open(outFile);
  fout.close();

  //open output file in append mode
  fout.open(outFile,ios::app);

  while(fin){
    readCharsToArray(fin,arr[0],n);
    transpose(arr[0],temp[0],n);
    for(int i=0; i<n; i++){
        for(int j=0; j<n; j++){
          if(temp[i][j]!='$')
            fout<<temp[i][j];
        }
    }
    memset(arr,'$',sizeof(arr));
  }
  fin.close();
  fout.close();
  
}

Sample output after running program on file "input.txt"

Following Sample output is shown on the following input.txt file:-

Output of file encrypt.txt is as following:-

Output of second run on file "encrypt.txt" is as following:

Output of decrypt.txt file is as following:-

Note - If any doubt/queries, do ask in comment section


Related Solutions

In this PYTHON 3 program assignment, you will find a text file named WorldSeries.txt. This file...
In this PYTHON 3 program assignment, you will find a text file named WorldSeries.txt. This file contains a chronological list of the World Series' winning teams from 1903 through 2018. The first line in the file is the name of the team that won in 1903, and the last line is the name of the team that won in 2018. (Note the World Series was not played in 1904 and 1994. There are entries in the file indicating this.) Write...
In C++ Write a program that contains a function, encrypt(Cypher) that encrypts the below text and...
In C++ Write a program that contains a function, encrypt(Cypher) that encrypts the below text and a second function, decrypt(Cypher),  that decrypts the encrypted message back to normal.  Cypher is the string which contain the plain or cypher texts.  Demonstrate that the encrypted message that you created is correctly decrypted.  For this problem you need to input “All Gaul is …” into a string Cypher.   Julius Caesar was one of the earliest persons to employ cryptology in history.  All his correspondence from his campaigns to...
ASSIGNMENT: Write a program and use the attached file (babynames.txt) as input file, and create two...
ASSIGNMENT: Write a program and use the attached file (babynames.txt) as input file, and create two output tiles. One file listing out all boys names, and the other file listing out all girls name. CODE: (teacher gave some of the code below use it to find the answer please String B is the boy names String E is girl names) import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.Scanner; /** This program reads a file with numbers, and writes the numbers...
ASSIGNMENT: Write a program and use the attached file (babynames.txt) as input file, and create two...
ASSIGNMENT: Write a program and use the attached file (babynames.txt) as input file, and create two output tiles. One file listing out all boys names, and the other file listing out all girls name. CODE: (teacher gave some of the code below use it to find the answer please String B is the boy names String E is girl names) import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.Scanner; /** This program reads a file with numbers, and writes the numbers...
You are given a text file containing a short text. Write a program that 1. Reads...
You are given a text file containing a short text. Write a program that 1. Reads a given text file : shortText.txt 2. Display the text as it is 3. Prints the number of lines 4. Prints the occurences of each letter that appears in the text. [uppercase and lowercase letter is treated the same]. 5. Prints the total number of special characters appear in the text. 6. Thedisplayofstep3,4and5aboveshouldbesaveinanoutputfile:occurencesText.txt write it in C++ programing Language
Write a C++ program to create a text file. Your file should contain the following text:...
Write a C++ program to create a text file. Your file should contain the following text: Batch files are text files created by programmer. The file is written in notepad. Creating a text file and writing to it by using fstream: to write to a file, you need to open thew file as write mode. To do so, include a header filr to your program. Create an object of type fsrteam. Open the file as write mode. Reading from a...
Write a C program that Reads a text file(any file)  and writes it to a binary file....
Write a C program that Reads a text file(any file)  and writes it to a binary file. Reads the binary file and converts it to a text file.
Could you write a c- program that reads a text file into a linked list of...
Could you write a c- program that reads a text file into a linked list of characters and then manipulate the linked list by making the following replacements 1. In paragraph 1 Replace all “c” with “s” if followed by the characters “e”, “i” or “y”; otherwise 2. In pragraph 2 Replace "We" with v"i" This is the text to be manipulated: Paragraph1 She told us to take the trash out. Why did she do that? I wish she would...
Could you write a c- program that reads a text file into a linked list of...
Could you write a c- program that reads a text file into a linked list of characters and then manipulate the linked list by making the following replacements 1. Replace all “c” with “s” if followed by the characters “e”, “i” or “y”; otherwise 2. Replace "sh" with ph This is the text to be manipulated: Paragraph1 She told us to take the trash out. Why did she do that? I wish she would not do that Paragraph 2 We...
C++ Code You will write a program to process the lines in a text file using...
C++ Code You will write a program to process the lines in a text file using a linked list and shared pointers. You will create a class “Node” with the following private data attributes: • Line – line from a file (string) • Next (shared pointer to a Node) Put your class definition in a header file and the implementation of the methods in a .cpp file. The header file will be included in your project. If you follow the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT