C++ help

profiledzaiki

If you were to encrypt a file using the cipher of Exercise P9.2, the letters will mixed up, and will appear as that you cannot decrypt it without the keyword. Also, it would seem illogical to try and guess the keyword, but someone who is trained in decryption would have no trouble breaking the cipher. The average letter frequencies of English letters are common knowledge. The most common letter E, occurs about 13% of the time.
 

  1. See Exhibit B.
  2. Write a program that reads as an input file and shows the letter frequencies in the file. This tool is invaluable to a code breaker. If the most frequent letters are H and K there is a high probability that they are the encryptions of E and T.

I have this code

 

#include<iostream>
#include<fstream>
#include<string>
using namespace std;
void count_frequencies(string str,int list[],int size)
{
    for(int i=0; i<str.length(); i++)
   {
       if(str[i]>='A' && str[i]<='Z')
       list[str[i]-'A']++;
       else if(str[i]>='a' && str[i]<='z')
       list[str[i]-'a']++;
       else continue;
   }
}
int main()
{
   ifstream infile;
   string file_name;
   int list[26] = { 0 };
   string str;
   cout <<"Enter file name to letter frequencies in the file. :";
   cin >> file_name; cout << endl;
    infile.open(file_name.c_str());
   if(!infile)
   {
       cout <<"Unable to open file . So Exiting from program :" << endl;
       return 0;
   }
   while(!infile.eof())
   {
       infile >> str;
       count_frequencies(str,list, 26);
   }
    infile.close();
   cout <<"Letter \t Count " << endl;
   int max = list[0];
   for(int i=0; i<26; i++)
   {
   cout << static_cast<char> (i+'a') <<" \t " << list[i] << endl;
   if(list[i] > max ) max = list[i];
   }
   cout <<"Most occured letter is " << static_cast<char> (max+'a') << endl;
    cout << "So E is Encrypted to " << static_cast<char> (max+'a') << endl;
   //system("pause");
   return 0;
}

 

but it does not work correclty.I need this ASAP

    • 8 years ago
    • 20
    Answer(2)

    Purchase the answer to view it

    blurred-text
    NOT RATED
    • attachment
      runningcode.png
    • attachment
      input.txt
    • attachment
      encrytion.doc

    Purchase the answer to view it

    blurred-text
    NOT RATED
    • attachment
      coding.zip
    • attachment
      kochulem.docx