Frequency f characters

Hello.
I am trying to make simple programm to decipher some crypted text (school excersise)
Text is crypted with substitution cipher, I have number of occurences of each letter in the original document.
This code calculates number of occurrences of each character in the crypted text. I need to arrange the results of these code not alphabetical as it does now, but by the number of occurences (from highiest to lowest), assign them to the number of occurrences of the original file and arrange them to the key from crypted file. Can anyone help me please? I am out of ideas to be honest (can provide text documents via email if needed)
Thank you in advance

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
  #include <iostream>
#include <fstream> //umožòuje tvoøit a zapisovat do souboru
#include <iomanip> //parametrické manipulace (fce. setw)
 
using namespace std;
 
 
int main()
{
    ofstream out_data("pocetnost.txt");
    int total[26] = { 0 };
 
    ifstream infile("fullzdroj.txt");
    if (!infile)
    {
        cout << "Chyba nacteni souboru." << endl;
        return 0;
    }
 
    char c;
    while (infile.get(c))         // pøeète znaky po jednom
    {
        if (isalpha(c))           // zkontroluje, zdali se jedná o malé èi velké písmena
        {
           
            int index = c - 'A';  // oznaèí je od 0 do 25
 
            total[index]++;       // vyèísli poèet opakování znakù
        }
    }
 
    for (int i = 0; i<26; i++)      // Napíše výsledek
    {
        cout << "  " << char(i + 'A') << " se vyskytuje: "
            << setw(5) << total[i] << " krat " << endl;
        out_data << "  " << char(i + 'A') << " se vyskytuje: "
            << setw(5) << total[i] << " krat" << endl;
    }
 
 
    return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <cctype>
#include <sstream>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;


int main()
{
   map<char,int> frequencies;

   stringstream ss( "This is my file: simulated, because I haven't got the original" );

   // Read from "file" into a character map
   char c;
   while( ss >> c ) if ( isalpha( c ) ) frequencies[ toupper( c ) ]++;

   // Put in a vector for sorting
   vector<pair<char,int>> temp( frequencies.size() );
   copy( frequencies.begin(), frequencies.end(), temp.begin() );

   // Sort on frequency of occurrence (HIGHEST first)
   sort( temp.begin(), temp.end(), []( auto p, auto q ){ return p.second > q.second; } );

   // Write out
   for ( auto p : temp ) cout << p.first << ": " << p.second << endl;
}
I: 7
E: 6
T: 5
A: 4
S: 4
H: 3
L: 3
U: 2
G: 2
M: 2
N: 2
O: 2
Y: 1
V: 1
R: 1
F: 1
D: 1
C: 1
B: 1
Last edited on
Topic archived. No new replies allowed.