I need some assistance or guy creating a C++ that can create a ciphertext for sp
ID: 3890160 • Letter: I
Question
I need some assistance or guy creating a C++ that can create a ciphertext for space and character
Statistics analyzer (ciphertext). It could be any ciphertext text in gerneral. I doesn't have to be a specific file from a program. Some help creating a program to a statistics analyzer for (plaintext) but i think i can use this same code to create a (ciphertext). i will paste the code.
Please create a program called sol_sac.ext, where ext denotes the file extension
corresponding to your choice of programming language (.py, .c, .cpp, .c++,
or .java).
This program should read in the file ciphertext.txt. The output should be
a file cipher_freq.txt which is much like corpus_freq.txt, but with ciphertext
characters rather than only letters and spaces. This is very similar to the
statistical analyzer for the corpus, and you should be able to reuse the code you
have from the previous task with very few changes. In fact there is a way to
arrange things so that this program is identical to sol_sap.ext. (But please
hand in two files to make the grading uniform.)
THis is the code for plaintext:
#include <iostream>
#include <fstream>
using namespace std;
int main() {
double num_char =0; // variable storing number of characters in a file
int sorted_index[26]; // array that will store the index of letter frequency in a sorted order
char ch; // variable to read a character from file
ifstream myfile("project Book.txt"); // file to open using ifstream object since we need to read the file
double char_freq[25]; //array that will store the relative letter frequency
for(int i=0;i<26;i++)
char_freq[i]=0;
if(myfile.is_open()) // checking if file exists
{
while (!myfile.eof()) // while end of file has not been reached
{
myfile.get(ch); // reading a character fom file
switch(ch){ // switch command to increase the frequency of the letter read irrespective of its case
case 'A' :
case 'a' : char_freq[0]++;
num_char++;
break;
case 'B' :
case 'b' : char_freq[1]++;
num_char++;
break;
case 'C' :
case 'c' : char_freq[2]++;
num_char++;
break;
case 'D' :
case 'd' : char_freq[3]++;
num_char++;
break;
case 'E' :
case 'e' : char_freq[4]++;
num_char++;
break;
case 'F' :
case 'f' : char_freq[5]++;
num_char++;
break;
case 'G' :
case 'g' : char_freq[6]++;
num_char++;
break;
case 'H' :
case 'h' : char_freq[7]++;
num_char++;
break;
case 'I' :
case 'i' : char_freq[8]++;
num_char++;
break;
case 'J' :
case 'j' : char_freq[9]++;
num_char++;
break;
case 'K' :
case 'k' : char_freq[10]++;
num_char++;
break;
case 'L' :
case 'l' : char_freq[11]++;
num_char++;
break;
case 'M' :
case 'm' : char_freq[12]++;
num_char++;
break;
case 'N' :
case 'n' : char_freq[13]++;
num_char++;
break;
case 'O' :
case 'o' : char_freq[14]++;
num_char++;
break;
case 'P' :
case 'p' : char_freq[15]++;
num_char++;
break;
case 'Q' :
case 'q' : char_freq[16]++;
num_char++;
break;
case 'R' :
case 'r' : char_freq[17]++;
num_char++;
break;
case 'S' :
case 's' : char_freq[18]++;
num_char++;
break;
case 'T' :
case 't' : char_freq[19]++;
num_char++;
break;
case 'U' :
case 'u' : char_freq[20]++;
num_char++;
break;
case 'V' :
case 'v' : char_freq[21]++;
num_char++;
break;
case 'W' :
case 'w' : char_freq[22]++;
num_char++;
break;
case 'X' :
case 'x' : char_freq[23]++;
num_char++;
break;
case 'Y' :
case 'y' : char_freq[24]++;
num_char++;
break;
case 'Z' :
case 'z' : char_freq[25]++;
num_char++;
break;
}
}
for(int i=0;i<26;i++) // calculating the relative frequency
{
char_freq[i]=char_freq[i]/num_char;
sorted_index[i]=i;
}
// sorting the frequencies in descending order
for(int i=0;i<26;i++)
{
for(int j=i+1;j<25;j++)
{
if(char_freq[sorted_index[i]]<char_freq[sorted_index[j]])
{
int temp = sorted_index[i];
sorted_index[i]=sorted_index[j];
sorted_index[j]=temp;
}
}
}
cout<<"The letter/frequency pairs in order of descending frequency:"<<endl;
for(int i=0;i<26;i++)
{
ch = sorted_index[i] +97; // to store the character whose frequency is displayed
cout<<ch<<","<<char_freq[sorted_index[i]]<<endl ;
}
}else{
cout<<"Unable to open file";
}
return 0;
}
Explanation / Answer
Here i have used one of the simplest of encodings in which a becomes b, b becomes c and z becomes a (both upper and lower case respectively).
And i have kept the the format similar to the code you have provided and done simple modifications and nothing else is encoded.Whitespace characters are kept the same and files names are dummy.You can change them as you desire.
#include <iostream>
#include <fstream>
using namespace std;
int main() {
char ch; // variable to read a character from file
ifstream myfile("project Book.txt"); // file to open using ifstream object since we need to read the file
if(myfile.is_open()) // checking if file exists
{
ofstream myfile;
myfile.open ("example.txt");
while (!myfile.eof()) // while end of file has not been reached
{
myfile.get(ch); // reading a character fom file
switch(ch){
case 'A' :
myfile << "B";
break;
case 'a' :
myfile << "b";
break;
case 'B' :
myfile << "C";
break;
case 'b' :
myfile << "c";
break;
case 'C' :
myfile << "D";
break;
case 'c' :
myfile << "d";
break;
case 'D' :
myfile << "E";
break;
case 'd' :
myfile << "e";
break;
case 'E' :
myfile << "F";
break;
case 'e' :
myfile << "f";
break;
case 'F' :
myfile << "G";
break;
case 'f' :
myfile << "g";
break;
case 'G' :
myfile << "H";
break;
case 'g' :
myfile << "h";
break;
case 'H' :
myfile << "I";
break;
case 'h' :
myfile << "i";
break;
case 'I' :
myfile << "J";
break;
case 'i' :
myfile << "j";
break;
case 'J' :
myfile << "K";
break;
case 'j' :
myfile << "k";
break;
case 'K' :
myfile << "L";
break;
case 'k' :
myfile << "l";
break;
case 'L' :
myfile << "M";
break;
case 'l' :
myfile << "m";
break;
case 'M' :
myfile << "N";
break;
case 'm' :
myfile << "n";
break;
case 'N' :
myfile << "O";
break;
case 'n' :
myfile << "o";
break;
case 'O' :
myfile << "P";
break;
case 'o' :
myfile << "p";
break;
case 'P' :
myfile << "Q";
break;
case 'p' :
myfile << "q";
break;
case 'Q' :
myfile << "R";
break;
case 'q' :
myfile << "r";
break;
case 'R' :
myfile << "S";
break;
case 'r' :
myfile << "s";
break;
case 'S' :
myfile << "T";
break;
case 's' :
myfile << "t";
break;
case 'T' :
myfile << "U";
break;
case 't' :
myfile << "u";
break;
case 'U' :
myfile << "V";
break;
case 'u' :
myfile << "v";
break;
case 'V' :
myfile << "W";
break;
case 'v' :
myfile << "w";
break;
case 'W' :
myfile << "X";
break;
case 'w' :
myfile << "x";
break;
case 'X' :
myfile << "Y";
break;
case 'x' :
myfile << "y";
break;
case 'Y' :
myfile << "Z";
break;
case 'y' :
myfile << "z";
break;
case 'Z' :
myfile << "A";
break;
case 'z' :
myfile << 'a';
break;
case default:
myfile << ch;
}
myfile.close();
}
return 0;
}